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

final class PCEC_Estimator_Core {
    const CORE_VERSION = '0.6.0';

    private static $manifest = ['industry'=>'painting','industry_label'=>'Painting','plugin_name'=>'Painter Contractor Ballpark Calculator','plugin_version'=>'5.1.3.2','core_version'=>'0.6.0','prefix'=>'PCEC','namespace'=>'QCD\PaintingEstimator','folder'=>'painter-contractor-ballpark-calculator','bootstrap_file'=>'painter-estimate-calculator.php','shortcode'=>'painter_estimate_calculator','post_type'=>'pcec_submission','option_name'=>'pcec_settings','meta_prefix'=>'_pcec_','settings_group'=>'pcec_group','qcd_source'=>'painting-calculator','qcd_endpoint_filter'=>'pcec_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);
    }
}
