"use strict";

import {Utils} from "../../Utils/Utils.ecma.js";
import {Check} from "../../Utils/Check.ecma.js";

/**
 * @typedef {import("../FormatModule.ecma.js").FormatModule} FormatModule
 */

/**
 * @class RangeFormat
 * @constructor
 * @param {!FormatModule} format 
 * @returns {void}
 * @access public
 * @static
 */
export const RangeFormat = (function(){

    /**
     * @constructs RangeFormat
     * @param {!FormatModule} format 
     * @returns {void}
     * @access private
     * @static
     */
    const RangeFormat = function(format){

        /** @type {RangeFormat} */
        const self = this;

        /**
         * @returns {void}
         * @access private
         */
        const constructor = () => {
            format.modes.range = self.get;
            format.checks.range = self.check;
        };

        /**
         * @param {[number, number]} coordenates 
         * @param {!(string|Array.<any|null>)} inputs 
         * @param {!Object.<string, any|null>} [shared = {}] 
         * @param {!Array.<string>} [fragments = []] 
         * @returns {string}
         * @access public
         */
        this.get = ([i, j], inputs, shared = {}, fragments = []) => {

            /** @type {Array.<number|[number, number]>} */
            const options = inputs.split("|").map(item => (
                      item.includes("-") ? item.split("-").map(Number) : 
                      Number(item)
                  )), 
                  /** @type {boolean} */
                  has_uniques = shared && shared.uniques_cache;
            /** @type {number} */
            let value;

            for(let _ = 0; _ < 100; _ ++){
                value = Check.is_number(value = Utils.get_random(options)) ? value : Utils.get_random(...value);
                if(!has_uniques || !shared.uniques_cache.includes("" + value))
                    break;
            };
            has_uniques && (shared.uniques_cache.push("" + value));

            // if(Check.is_number(inputs = (
            //     Check.is_string(inputs) ? Utils.get_random(inputs.split("|")).split("-").map(Number) : 
            //     Check.is_array(inputs) ? inputs : 
            // [])))
            //     return "" + inputs;

            // /** @type {number} */
            // const l = inputs.length;

            // return "" + (
            //     !l ? null : 
            //     l == 1 ? inputs[0] : 
            // Utils.get_random(...inputs));
            return value || 0;
        };

        /**
         * @param {[number, number]} coordenates 
         * @param {!string} string 
         * @param {!(string|Array.<string>)} inputs 
         * @param {!Object.<string, any|null>} [shared = {}] 
         * @param {!Array.<string>} [fragments = []] 
         * @returns {number}
         * @access public
         */
        this.check = ([i, j], string, inputs, shared = {}, fragments = []) => FormatModule.check_range(string, inputs);

        constructor();

    };

    return RangeFormat;
})();