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

namespace craft\commerce\web\assets\commercecp;

use Craft;
use craft\commerce\behaviors\StoreBehavior;
use craft\commerce\models\ProductType;
use craft\commerce\Plugin;
use craft\helpers\Json;
use craft\models\Site;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;
use craft\web\View;
use yii\web\JqueryAsset;

/**
 * Asset bundle for the Control Panel
 *
 * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
 * @since 2.0
 */
class CommerceCpAsset extends AssetBundle
{
    /**
     * @inheritdoc
     */
    public function init(): void
    {
        $this->sourcePath = __DIR__ . '/dist';

        $this->depends = [
            CpAsset::class,
            JqueryAsset::class,
        ];

        // These files are generated by gulp
        $this->css[] = 'css/commercecp.css';
        $this->js[] = 'commercecp.js';

        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function registerAssetFiles($view): void
    {
        parent::registerAssetFiles($view);

        if ($view instanceof View) {
            $view->registerTranslations('commerce', [
                'Add Address',
                'Add',
                'Address Line 1',
                'Address Line 2',
                'Address Updated.',
                'Alternative Phone',
                'Cancel',
                'City',
                'Country',
                'Download Type',
                'Download',
                'Edit',
                'First Name',
                'Last Name',
                'Message',
                'New product',
                'New product, choose a type',
                'New {productType} product',
                'New',
                'No Address',
                'Organization Name',
                'Organization Tax ID',
                'PDF',
                'Phone (Alt)',
                'Phone',
                'State',
                'Status Updated.',
                'Status change message',
                'Update Address',
                'Update Order Status',
                'Update',
                'Update',
                'Zip Code',
            ]);
        }

        // Define the Craft.Commerce object
        $commerceData = $this->_commerceData();
        $editableProductTypes = Json::encode($commerceData['editableProductTypes']);
        $sitesStores = Json::encode($commerceData['sitesStores']);

        $js = <<<JS
if (typeof window.Craft.Commerce === typeof undefined) {
    window.Craft.Commerce = {};
}

window.Craft.Commerce.editableProductTypes = $editableProductTypes;
window.Craft.Commerce.sitesStores = $sitesStores;
JS;
        $view->registerJs($js, View::POS_HEAD);
    }

    private function _commerceData(): array
    {
        $sitesStores = [];
        foreach (Craft::$app->getSites()->getAllSites() as $site) {
            /** @var Site|StoreBehavior $site */
            $sitesStores[$site->id] = $site->getStore()->id;
        }

        return [
            'editableProductTypes' => array_map(fn(ProductType $productType) => [
                'id' => $productType->id,
                'uid' => $productType->uid,
                'name' => Craft::t('site', $productType->name),
                'handle' => $productType->handle,
            ], Plugin::getInstance()->getProductTypes()->getCreatableProductTypes()),
            'sitesStores' => $sitesStores,
        ];
    }
}
