<?php

declare(strict_types=1);

namespace Drupal\flowdrop_node_category;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Provides a listing of flowdrop node categories.
 */
final class FlowDropNodeCategoryListBuilder extends ConfigEntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader(): array {
    $header['label'] = $this->t('Label');
    $header['id'] = $this->t('Machine name');
    $header['status'] = $this->t('Status');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity): array {
    /** @var \Drupal\flowdrop_node_category\FlowDropNodeCategoryInterface $entity */
    $row['label'] = $entity->label();
    $row['id'] = $entity->id();
    $row['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity): array {
    $operations = [];

    $entityId = $entity->id();
    if (is_string($entityId)) {
      // Edit operation.
      $operations["edit"] = [
        "title" => $this->t("Edit"),
        "url" => Url::fromRoute("flowdrop.config.category.edit", [
          "flowdrop_node_category" => $entityId,
        ]),
        "weight" => 10,
      ];

      // Delete operation.
      $operations["delete"] = [
        "title" => $this->t("Delete"),
        "url" => Url::fromRoute("flowdrop.config.category.delete", [
          "flowdrop_node_category" => $entityId,
        ]),
        "weight" => 100,
      ];
    }

    return $operations;
  }

}
