<?php
/**
 * NOTICE OF LICENSE.
 *
 * Copyright 2022 Kosmonaft.dev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  @author    Alexandre DEBUSSCHÈRE <alexandre@kosmonaft.dev>
 *  @copyright 2022 - 2023 Kosmonaft.dev
 *  @license   MIT
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Class MRMaxCountry
 */
class MRMaxCountry extends Country
{

    /** @var int[] */
    public static $iso_postcode = [
        'GP' => 971,
        'GF' => 973,
        'PM' => 975,
        'MQ' => 972,
        'RE' => 974,
        'YT' => 976,
        'TF' => 984,
        'PF' => 987,
        'WF' => 986,
        'NC' => 988
    ];

    /** @var string[] */
    public static $enabled_pickup_country = [
        'FR',
        'BE',
        'DE',
        'NL',
        'LU',
        'ES',
        'GB',
        'PT',
        'AT',
        'EE',
        'LV',
        'LT',
        'CZ',
        'HU',
        'SK',
        'SI',
        'DK',
        'FI',
        'IE',
        'PL',
        'SE'
    ];

    /**
     * Sometimes, customer set an address for DROM/COM but with country FR instead of GP for instance.
     * Often happens if the DROM/COM country is not enabled on PrestaShop.
     *
     * @param Address $address
     * @return false|int|string
     */
    public static function getIsoByAddress(Address $address)
    {
        $country_iso_code = Country::getIsoById((int)$address->id_country);
        if ($country_iso_code != 'FR' || $address->postcode < 97000) {
            return $country_iso_code;
        }

        // Outre-Mer check
        $prefix = Tools::substr((string)$address->postcode, 0, 3);

        return array_search($prefix, self::$iso_postcode);
    }

    /**
     * @param string $iso_country
     * @return bool
     */
    public static function isVolumetricWeight($iso_country)
    {
        return !in_array($iso_country, [
            'FR',
            'DE',
            'AT',
            'BE',
            'DK',
            'ES',
            'EE',
            'HU',
            'IE',
            'IT',
            'LT',
            'LV',
            'LU',
            'NL',
            'PL',
            'PT',
            'CZ',
            'RO',
            'GB',
            'SK',
            'SI',
            'SE',
            'CG'
        ]);
    }

    /**
     * @return string[]
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function getEnabledCountriesForPickup()
    {
        return array_reduce(self::$enabled_pickup_country, function ($list, $iso) {
            $country = new Country(Country::getByIso($iso));
            if (Validate::isLoadedObject($country) && $country->active) {
                $list[] = $iso;
            }

            return $list;
        }, []);
    }
}
