<?php
if (!defined('ABSPATH')) exit;

final class RFEC_Estimator_Core {
    const CORE_VERSION = '0.6.0';

    private static $manifest = ['industry'=>'roofing','industry_label'=>'Roof','plugin_name'=>'Contractor Roof Estimate Calculator','plugin_version'=>'5.1.7.4','core_version'=>'0.6.0','prefix'=>'RFEC','namespace'=>'QCD\RoofingEstimator','folder'=>'contractor-roof-estimate-calculator','bootstrap_file'=>'roof-estimate-calculator.php','shortcode'=>'roof_estimate_calculator','post_type'=>'rfec_submission','option_name'=>'rfec_settings','meta_prefix'=>'_rfec_','settings_group'=>'rfec_group','qcd_source'=>'roof-calculator','qcd_endpoint_filter'=>'rfec_qcd_endpoint_url','qcd_endpoint'=>'https://quietcitydesign.com/wp-json/qcd/v1/estimator-event'];

    public static function manifest($key = null, $fallback = null) {
        if ($key === null) return self::$manifest;
        return array_key_exists($key, self::$manifest) ? self::$manifest[$key] : $fallback;
    }

    public static function money($amount, array $settings, $decimals = 0) {
        $symbol = isset($settings['currency_symbol']) ? (string)$settings['currency_symbol'] : '$';
        $currency = isset($settings['currency']) ? (string)$settings['currency'] : '';
        return $symbol.number_format_i18n((float)$amount, absint($decimals)).($currency !== '' ? ' '.$currency : '');
    }

    public static function language(array $settings) {
        $language = isset($settings['language']) ? (string)$settings['language'] : '';
        if (in_array($language, ['en_CA', 'en_US'], true)) return $language;
        return (($settings['market'] ?? 'CA') === 'US') ? 'en_US' : 'en_CA';
    }

    public static function regionalize($content, array $settings) {
        $content = (string)$content;
        $replace = self::language($settings) === 'en_US' ? [
                'Colours'=>'Colors', 'colours'=>'colors', 'Colour'=>'Color', 'colour'=>'color',
                'Labour'=>'Labor', 'labour'=>'labor',
                'Storeys'=>'Stories', 'storeys'=>'stories', 'Storey'=>'Story', 'storey'=>'story',
                'Postal / ZIP code'=>'ZIP code', 'Postal code'=>'ZIP code', 'postal code'=>'ZIP code',
                'Moulding'=>'Molding', 'moulding'=>'molding',
            ] : [
            'Colors'=>'Colours', 'colors'=>'colours', 'Color'=>'Colour', 'color'=>'colour',
            'Labor'=>'Labour', 'labor'=>'labour',
            'Stories'=>'Storeys', 'stories'=>'storeys', 'Story'=>'Storey', 'story'=>'storey',
            'ZIP code'=>'Postal code',
            'Molding'=>'Moulding', 'molding'=>'moulding',
        ];
        if (strpos($content, '<') === false) return strtr($content, $replace);

        $protected = [];
        $content = preg_replace_callback('/<(script|style)\b[^>]*>.*?<\/\1>/is', static function($match) use (&$protected) {
            $token = '%%QCD_REGION_BLOCK_'.count($protected).'%%';
            $protected[$token] = $match[0];
            return $token;
        }, $content);
        $parts = preg_split('/(<[^>]+>)/s', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($parts as &$part) {
            if ($part === '' || $part[0] === '<') continue;
            $part = strtr($part, $replace);
        }
        unset($part);
        return strtr(implode('', $parts), $protected);
    }

    public static function option_name() {
        return (string)self::manifest('option_name');
    }

    public static function post_type() {
        return (string)self::manifest('post_type');
    }

    public static function meta_key($key) {
        return (string)self::manifest('meta_prefix').sanitize_key($key);
    }
}
