<?php
/**
 * @link https://craftcms.com/
 * @copyright Copyright (c) Pixel & Tonic, Inc.
 * @license https://craftcms.github.io/license/
 */

namespace craft\elements\actions;

use Craft;
use craft\base\ElementAction;
use craft\base\ElementInterface;
use craft\base\NestedElementInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\Db;
use Throwable;

/**
 * Duplicate represents a Duplicate element action.
 *
 * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
 * @since 3.0.30
 */
class Duplicate extends ElementAction
{
    /**
     * @var bool Whether to also duplicate the selected elements’ descendants
     */
    public bool $deep = false;

    /**
     * @var bool Whether to duplicate the selected elements as drafts
     * @since 5.9.0
     */
    public bool $asDrafts = false;

    /**
     * @var string|null The message that should be shown after the elements get deleted
     */
    public ?string $successMessage = null;

    /**
     * @inheritdoc
     */
    public function getTriggerLabel(): string
    {
        return $this->deep
            ? Craft::t('app', 'Duplicate (with descendants)')
            : Craft::t('app', 'Duplicate');
    }

    /**
     * @inheritdoc
     * @since 3.5.0
     */
    public function getTriggerHtml(): ?string
    {
        // Only enable for duplicatable elements, per canDuplicate()
        Craft::$app->getView()->registerJsWithVars(fn($type, $attr) => <<<JS
(() => {
  new Craft.ElementActionTrigger({
    type: $type,
    validateSelection: (selectedItems, elementIndex) => {
      for (let i = 0; i < selectedItems.length; i++) {
        if (!Garnish.hasAttr(selectedItems.eq(i).find('.element'), $attr)) {
          return false;
        }
      }

      return elementIndex.settings.canDuplicateElements(selectedItems);
    },
    beforeActivate: async (selectedItems, elementIndex) => {
      await elementIndex.onBeforeDuplicateElements(selectedItems);
    },
    afterActivate: async (selectedItems, elementIndex) => {
      await elementIndex.onDuplicateElements(selectedItems);
    },
  });
})();
JS, [
            static::class,
            $this->asDrafts ? 'data-duplicatable-as-draft' : 'data-duplicatable',
        ]);

        return null;
    }

    /**
     * @inheritdoc
     */
    public function performAction(ElementQueryInterface $query): bool
    {
        if ($this->deep) {
            $query->orderBy(['structureelements.lft' => SORT_ASC]);
        }

        $successCount = 0;
        $failCount = 0;

        $this->_duplicateElements($query, $successCount, $failCount);

        // Did all of them fail?
        if ($successCount === 0) {
            $this->setMessage(Craft::t('app', 'Could not duplicate elements due to validation errors.'));
            return false;
        }

        if ($failCount !== 0) {
            $this->setMessage(Craft::t('app', 'Could not duplicate all elements due to validation errors.'));
        } else {
            $this->setMessage(Craft::t('app', 'Elements duplicated.'));
        }

        return true;
    }

    /**
     * @param ElementQueryInterface $query
     * @param array<int|string, bool> $duplicatedElementIds
     * @param int $successCount
     * @param int $failCount
     * @param ElementInterface|null $newParent
     */
    private function _duplicateElements(ElementQueryInterface $query, int &$successCount, int &$failCount, array &$duplicatedElementIds = [], ?ElementInterface $newParent = null): void
    {
        $elementsService = Craft::$app->getElements();
        $structuresService = Craft::$app->getStructures();
        $user = Craft::$app->getUser()->getIdentity();

        foreach (Db::each($query) as $element) {
            $allowed = $this->asDrafts
                ? $elementsService->canDuplicateAsDraft($element, $user)
                : $elementsService->canDuplicate($element, $user);

            if (!$allowed) {
                continue;
            }

            // Make sure this element wasn't already duplicated, which could
            // happen if it's the descendant of a previously duplicated element
            // and $this->deep == true.
            if (isset($duplicatedElementIds[$element->id])) {
                continue;
            }

            $attributes = [
                'isProvisionalDraft' => false,
                'draftId' => null,
            ];

            // If the element was loaded for a non-primary owner, set its primary owner to it
            if ($element instanceof NestedElementInterface) {
                $attributes['primaryOwner'] = $element->getOwner();
                $attributes['sortOrder'] = null; // clear our sort order too
            }

            try {
                $duplicate = $elementsService->duplicateElement(
                    $element,
                    $attributes,
                    asUnpublishedDraft: $this->asDrafts,
                );
            } catch (Throwable) {
                // Validation error
                $failCount++;
                continue;
            }

            $successCount++;
            $duplicatedElementIds[$element->id] = true;

            if ($newParent) {
                // Append it to the duplicate of $element’s parent
                $structuresService->append($element->structureId, $duplicate, $newParent);
            } elseif ($element->structureId) {
                // Place it right next to the original element
                $structuresService->moveAfter($element->structureId, $duplicate, $element);
            }

            if ($this->deep) {
                // Don't use $element->children() here in case its lft/rgt values have changed
                $childQuery = $element::find()
                    ->siteId($element->siteId)
                    ->descendantOf($element->id)
                    ->descendantDist(1)
                    ->status(null);

                $this->_duplicateElements($childQuery, $successCount, $failCount, $duplicatedElementIds, $duplicate);
            }
        }
    }
}
