'use strict';

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

var __assign = function() {
    __assign = Object.assign || function __assign(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};

var KEY_MATCH = 'INCONTEXT-KEY-MATCH';
var INCONTEXT_KEY_REG = /^\s*\[\[__starling_(\d+)_(.*)__\]\]\s*$/;
/**
 * 目前IN-CONTEXT支持两种模式
 * 1：源文案匹配（默认）
 * 2：key匹配
 */
var supportKeyMatch = function () { return document.getElementById(KEY_MATCH); };
if (typeof window !== 'undefined') {
    window['__STARLING_INCONTEXT_GLOBAL__HOOK__'] = __assign({}, (window['__STARLING_INCONTEXT_GLOBAL__HOOK__'] || { payload: {} }));
}
var index = {
    name: 'inContext',
    type: 'postProcessor',
    process: function (value, key, options, translator) {
        // 必须是浏览器环境 且 安装了Starling In-Context插件 (__STARLING_INCONTEXT_FLAG__ 为插件翻译标志)
        if (window && supportKeyMatch() && !options['__STARLING_INCONTEXT_FLAG__']) {
            /**
             * TODO: 所以I18N均可以接入 将功能抽离出一份通用sdk
             * t函数 (语言翻译由脚本来修改)
             * 不能直接给插件翻译 (无法传递过去)
             */
            if (!window['__STARLING_INCONTEXT_GLOBAL_I18N__']) {
                window['__STARLING_INCONTEXT_GLOBAL_I18N__'] = translator;
            }
            delete options.keySeparator;
            delete options.lngs;
            var index = 0;
            key = key.toString();
            // fixed: I18n.t嵌套使用时
            if (INCONTEXT_KEY_REG.test(key)) {
                var matchData = key.match(INCONTEXT_KEY_REG);
                matchData[0]; var _index = matchData[1], _key = matchData[2];
                index = _index;
                key = _key;
            }
            if (INCONTEXT_KEY_REG.test(value)) {
                var matchData = value.match(INCONTEXT_KEY_REG);
                matchData[0]; var _index = matchData[1], _key = matchData[2];
                value = _key;
            }
            if (!window['__STARLING_INCONTEXT_GLOBAL__HOOK__'].payload[key]) {
                window['__STARLING_INCONTEXT_GLOBAL__HOOK__'].payload[key] = [
                    {
                        options: options,
                        default: value
                    }
                ];
                index = 0;
            }
            else {
                // TODO: 规避时间戳(参数变化太快导致无用的累加)
                var findTIndex = window['__STARLING_INCONTEXT_GLOBAL__HOOK__'].payload[key].findIndex(function (item) { return JSON.stringify(item.options) === JSON.stringify(options); });
                if (findTIndex > -1) {
                    window['__STARLING_INCONTEXT_GLOBAL__HOOK__'].payload[key][findTIndex].default = value;
                    index = findTIndex;
                }
                else {
                    index =
                        window['__STARLING_INCONTEXT_GLOBAL__HOOK__'].payload[key].push({
                            options: options,
                            default: value
                        }) - 1;
                }
            }
            return "[[__starling_" + index + "_" + key + "__]]";
        }
        else {
            return value;
        }
    }
};

module.exports = index;
