"use strict";

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

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

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

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

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

        /**
         * @returns {void}
         * @access private
         */
        const constructor = () => {

            format.modes.mix = self.get;
            format.checks.mix = self.check;

            format.modes.Mix = ([i, j], inputs, shared, fragments) => self.get([i, j], inputs, {
                ...shared, capitalized : true
            }, fragments);
            format.checks.Mix = ([i, j], string, inputs, shared, fragments) => self.check([i, j], string, inputs, {
                ...shared, capitalized : true
            }, fragments);

            ["mix_uniques", "mixUniques", "mixuniques"].forEach(key => {
                format.modes[key] = ([i, j], inputs, shared, fragments) => self.get([i, j], inputs, {
                    ...shared, uniques : true
                }, fragments);
                format.checks[key] = ([i, j], string, inputs, shared, fragments) => self.check([i, j], string, inputs, {
                    ...shared, uniques : true
                }, fragments);
            });

            ["Mix_uniques", "Mix_Uniques", "MixUniques"].forEach(key => {
                format.modes[key] = ([i, j], inputs, shared, fragments) => self.get([i, j], inputs, {
                    ...shared, capitalized : true, uniques : true
                }, fragments);
                format.checks[key] = ([i, j], string, inputs, shared, fragments) => self.check([i, j], string, inputs, {
                    ...shared, capitalized : true, uniques : true
                }, fragments);
            });

        };

        /**
         * @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 {[string, Array.<string>]} */
            const [separator, original_items] = (
                Check.is_string(inputs) ? ((_, separator, options) => [
                    separator, options.split("|")
                ])(...inputs.match(/^([^,]+),(.*)$/)) :
                Check.is_array(inputs) ? inputs : 
            []);

            return format.get_items([i, j], original_items, original_items.length, separator, fragments, shared);
        };

        /**
         * @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 = []) => {

            /** @type {[string, Array.<string>]} */
            const [separator, original_items] = (
                      Check.is_string(inputs) ? ((_, separator, items) => [
                          separator, items.split("|")
                      ])(...inputs.match(/^([^,]+),(.*)$/)) :
                      Check.is_array(inputs) ? inputs : 
                  []), 
                  /** @type {Array.<string>} */
                  items = format.get_list([i, j], [...original_items]), 
                  /** @type {number} */
                  l = items.length;

            return format.check_select([i, j], string, [[l, l], separator, items], shared, fragments);
        };

        constructor();

    };

    return MixFormat;
})();