<?php
/**
 * Shop template kinds Bricks needs for a WooCommerce storefront.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Support;

/**
 * Maps conversational shop kinds to Bricks wc_* template types and default conditions.
 */
final class WooShopTemplates
{
    /**
     * @return array<string, array{kind: string, bricks_type: string, label: string, conditions: array<int, array<string, mixed>>}>
     */
    public static function kinds(): array
    {
        return [
            'product' => [
                'kind'        => 'product',
                'bricks_type' => 'wc_product',
                'label'       => 'Single product',
                'conditions'  => [
                    ['main' => 'postType', 'postType' => ['product']],
                ],
            ],
            'archive' => [
                'kind'        => 'archive',
                'bricks_type' => 'wc_archive',
                'label'       => 'Product archive',
                'conditions'  => [
                    ['main' => 'archiveType', 'archiveType' => 'product'],
                ],
            ],
            'cart' => [
                'kind'        => 'cart',
                'bricks_type' => 'wc_cart',
                'label'       => 'Cart',
                'conditions'  => [],
            ],
            'checkout' => [
                'kind'        => 'checkout',
                'bricks_type' => 'wc_form_checkout',
                'label'       => 'Checkout',
                'conditions'  => [],
            ],
            'account' => [
                'kind'        => 'account',
                'bricks_type' => 'wc_account_dashboard',
                'label'       => 'My account',
                'conditions'  => [],
            ],
        ];
    }

    /**
     * @return array{kind: string, bricks_type: string, label: string, conditions: array<int, array<string, mixed>>}|null
     */
    public static function resolve(string $raw): ?array
    {
        $key = sanitize_key($raw);
        $aliases = [
            'product'     => 'product',
            'single'      => 'product',
            'ficha'       => 'product',
            'wc_product'  => 'product',
            'archive'     => 'archive',
            'shop'        => 'archive',
            'tienda'      => 'archive',
            'wc_archive'  => 'archive',
            'cart'        => 'cart',
            'carrito'     => 'cart',
            'wc_cart'     => 'cart',
            'checkout'    => 'checkout',
            'finalizar'   => 'checkout',
            'wc_form_checkout' => 'checkout',
            'account'     => 'account',
            'cuenta'      => 'account',
            'my-account'  => 'account',
            'my_account'  => 'account',
            'wc_account_dashboard' => 'account',
        ];

        $kind = $aliases[$key] ?? '';
        if ($kind === '') {
            return null;
        }

        return self::kinds()[$kind] ?? null;
    }

    /**
     * @return string[]
     */
    public static function bricksTypes(): array
    {
        return array_values(array_map(
            static fn (array $kind): string => $kind['bricks_type'],
            self::kinds()
        ));
    }
}
