<?php

if (!function_exists('bigmint_markets_config')) {
    /**
     * Canonical market definitions: slug => label + enable flag.
     * Order preserved for menus (PHP maintains insertion order).
     *
     * @return array<string, array{label:string, active:int}>
     */
    function bigmint_markets_config()
    {
        return [
            'ferrous' => ['label' => 'Ferrous', 'active' => 1],
            'non-ferrous' => ['label' => 'Non Ferrous', 'active' => 1],
            'coal' => ['label' => 'Coal & Energy', 'active' => 1],
            'scrap-recycling' => ['label' => 'Scrap Recycling', 'active' => 1],
            'agriculture' => ['label' => 'Agriculture', 'active' => 1],
            'logistics' => ['label' => 'Logistics', 'active' => 1]
        ];
    }
}

if (!function_exists('bigmint_market_definitions')) {
    /**
     * Same data as {@see bigmint_markets_config()} as a zero-indexed list (slug + label + active).
     *
     * @return array<int, array{slug:string, label:string, active:int}>
     */
    function bigmint_market_definitions()
    {
        $out = [];
        foreach (bigmint_markets_config() as $slug => $meta) {
            $out[] = [
                'slug' => $slug,
                'label' => $meta['label'],
                'active' => (int) $meta['active'],
            ];
        }

        return $out;
    }
}

if (!function_exists('bigmint_markets_map')) {
    /**
     * Slug => display label (all entries; ignore active — use {@see bigmint_markets_map_active()} if needed).
     *
     * @return array<string, string>
     */
    function bigmint_markets_map()
    {
        $out = [];
        foreach (bigmint_markets_config() as $slug => $meta) {
            $out[$slug] = $meta['label'];
        }

        return $out;
    }
}

if (!function_exists('bigmint_markets_map_active')) {
    /**
     * Slug => label for rows where active === 1.
     *
     * @return array<string, string>
     */
    function bigmint_markets_map_active()
    {
        $out = [];
        foreach (bigmint_markets_config() as $slug => $meta) {
            if (!empty($meta['active'])) {
                $out[$slug] = $meta['label'];
            }
        }

        return $out;
    }
}

if (!function_exists('bigmint_market_slug_to_label')) {
    /**
     * Resolve display label for a slug, or empty string if unknown.
     *
     * @param string $slug
     *
     * @return string
     */
    function bigmint_market_slug_to_label($slug)
    {
        $normalized = strtolower(trim((string) $slug));
        if ($normalized === 'nonferrous' || $normalized === 'non_ferrous') {
            $normalized = 'non-ferrous';
        }
        $cfg = bigmint_markets_config();

        return isset($cfg[$normalized]['label']) ? $cfg[$normalized]['label'] : '';
    }
}

if (!function_exists('bigmint_market_slugs')) {
    /**
     * Ordered slug list (all entries).
     *
     * @return array<int, string>
     */
    function bigmint_market_slugs()
    {
        return array_keys(bigmint_markets_config());
    }
}
