<?php
/**
 * Catalog of Bricks WooCommerce elements grouped by shop function.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Support;

/**
 * Bricks WooCommerce elements (one file per element; base.php is not insertable). Insertion stays on insertElement.
 */
final class WooProductCatalog
{
    /**
     * @return array<string, array{label: string, elements: array<string, string>}>
     */
    public static function groups(): array
    {
        return [
            'product' => [
                'label'    => 'Single product',
                'elements' => [
                    'product-title'                   => 'Product title',
                    'product-price'                   => 'Product price',
                    'product-gallery'                 => 'Product gallery',
                    'product-add-to-cart'             => 'Add to cart',
                    'product-rating'                  => 'Product rating',
                    'product-stock'                   => 'Product stock',
                    'product-short-description'       => 'Short description',
                    'product-content'                 => 'Product content',
                    'product-meta'                    => 'Product meta',
                    'product-tabs'                    => 'Product tabs',
                    'product-reviews'                 => 'Product reviews',
                    'product-additional-information'  => 'Additional information',
                    'product-related'                 => 'Related products',
                    'product-upsells'                 => 'Upsells',
                ],
            ],
            'archive' => [
                'label'    => 'Shop archive',
                'elements' => [
                    'woocommerce-products'                    => 'Products',
                    'woocommerce-products-archive-description' => 'Archive description',
                    'woocommerce-products-filter'             => 'Products filter',
                    'woocommerce-products-orderby'            => 'Order by',
                    'woocommerce-products-pagination'         => 'Pagination',
                    'woocommerce-products-total-results'      => 'Total results',
                ],
            ],
            'cart' => [
                'label'    => 'Cart',
                'elements' => [
                    'woocommerce-cart-items'       => 'Cart items',
                    'woocommerce-cart-coupon'      => 'Cart coupon',
                    'woocommerce-cart-collaterals' => 'Cart totals',
                    'woocommerce-mini-cart'        => 'Mini cart',
                ],
            ],
            'checkout' => [
                'label'    => 'Checkout',
                'elements' => [
                    'woocommerce-checkout-coupon'            => 'Checkout coupon',
                    'woocommerce-checkout-customer-details'  => 'Customer details',
                    'woocommerce-checkout-login'             => 'Checkout login',
                    'woocommerce-checkout-order-payment'     => 'Order payment',
                    'woocommerce-checkout-order-review'      => 'Order review',
                    'woocommerce-checkout-order-table'       => 'Order table',
                    'woocommerce-checkout-thankyou'          => 'Thank you',
                ],
            ],
            'account' => [
                'label'    => 'My account',
                'elements' => [
                    'woocommerce-account-page'                 => 'Account page',
                    'woocommerce-account-orders'               => 'Orders',
                    'woocommerce-account-view-order'           => 'View order',
                    'woocommerce-account-downloads'            => 'Downloads',
                    'woocommerce-account-addresses'            => 'Addresses',
                    'woocommerce-account-form-edit-account'    => 'Edit account',
                    'woocommerce-account-form-edit-address'    => 'Edit address',
                    'woocommerce-account-form-login'           => 'Login form',
                    'woocommerce-account-form-register'        => 'Register form',
                    'woocommerce-account-form-lost-password'   => 'Lost password',
                    'woocommerce-account-form-reset-password'  => 'Reset password',
                    'woocommerce-account-payment-methods'      => 'Payment methods',
                    'woocommerce-account-add-payment-method'   => 'Add payment method',
                ],
            ],
            'shared' => [
                'label'    => 'Shared shop pieces',
                'elements' => [
                    'woocommerce-breadcrumbs'    => 'Breadcrumbs',
                    'woocommerce-notice'         => 'Shop notice',
                    'woocommerce-template-hook'  => 'Template hook',
                ],
            ],
        ];
    }

    /**
     * @return array<string, string>
     */
    public static function names(): array
    {
        $names = [];
        foreach (self::groups() as $group) {
            foreach ($group['elements'] as $name => $label) {
                $names[$name] = $label;
            }
        }

        return $names;
    }

    public static function count(): int
    {
        return count(self::names());
    }

    public static function groupFor(string $name): string
    {
        foreach (self::groups() as $groupId => $group) {
            if (isset($group['elements'][$name])) {
                return $groupId;
            }
        }

        return '';
    }

    public static function resolveElement(string $raw): string
    {
        $key = sanitize_key($raw);
        if ($key === '') {
            return '';
        }

        $names = self::names();
        if (isset($names[$key])) {
            return $key;
        }

        $aliases = [
            'title'                    => 'product-title',
            'product_title'            => 'product-title',
            'price'                    => 'product-price',
            'product_price'            => 'product-price',
            'gallery'                  => 'product-gallery',
            'product_gallery'          => 'product-gallery',
            'add_to_cart'              => 'product-add-to-cart',
            'addtocart'                => 'product-add-to-cart',
            'rating'                   => 'product-rating',
            'stock'                    => 'product-stock',
            'reviews'                  => 'product-reviews',
            'related'                  => 'product-related',
            'tabs'                     => 'product-tabs',
            'meta'                     => 'product-meta',
            'short_description'        => 'product-short-description',
            'excerpt'                  => 'product-short-description',
            'content'                  => 'product-content',
            'description'              => 'product-content',
            'additional_information'   => 'product-additional-information',
            'additional'               => 'product-additional-information',
            'upsells'                  => 'product-upsells',
        ];

        return $aliases[$key] ?? '';
    }
}
