"use strict";

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

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

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

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

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

        /**
         * @returns {void}
         * @access private
         */
        const constructor = () => {
            format.modes.plain = self.get;
            format.checks.plain = 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 {string} */
            let results = format.execute([i, j], 
                Check.is_string(inputs) ? inputs : 
                Check.is_string(inputs[0]) ? inputs[0] : 
            "", shared, fragments);

            return FormatModule.process_item(results, 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} */
            const substring = format.set_fragments_level((
                Check.is_string(inputs) ? inputs.split("|") : 
                Check.is_string(inputs[0]) ? inputs[0] : 
            ""), fragments);

            return (
                /\{[^\{\}]+\}/.test(format.set_fragments_level(substring, fragments)) ? format.get_check_length([i, j], string, [substring], shared, fragments, false) : 
            FormatModule.prepare_result(string, substring, shared));
        };

        constructor();

    };

    return PlainFormat;
})();