'use strict';

var React = require('react');
var translationsContext = require('./translationsContext-78113889.js');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n["default"] = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

// SRC: https://github.com/berezh/react-string-format
function format(text, params) {
    let result = [text];
    Object.keys(params).forEach((key) => {
        result = parseAndReplace(result, params[key], key);
    });
    if (result.length === 0) {
        return '';
    }
    else if (result.length === 1 && typeof result[0] === 'string') {
        return result[0];
    }
    else if (result.every((x) => typeof x === 'string')) {
        return result.join('');
    }
    else {
        return (React__namespace.createElement(React__namespace.Fragment, null, result.map((x, i) => {
            return React__namespace.createElement(React__namespace.Fragment, { key: i }, x);
        })));
    }
}
function replaceWhiteSpace(text) {
    const result = [];
    let start = false;
    let end = false;
    if (!!text.match(/^\s+/gi)) {
        text = text.replace(/^\s+/gi, '');
        start = true;
    }
    if (!!text.match(/\s+$/gi)) {
        text = text.replace(/\s+$/gi, '');
        end = true;
    }
    if (start) {
        result.push(React__namespace.createElement(React__namespace.Fragment, null, "\u00A0"));
    }
    result.push(text);
    if (end) {
        result.push(React__namespace.createElement(React__namespace.Fragment, null, "\u00A0"));
    }
    return result;
}
function parseAndReplace(source, replaceWith, key) {
    const result = [];
    source.forEach((possibleText) => {
        if (typeof possibleText === 'string') {
            const pattern = new RegExp(`\\{${key}\\}`, 'gi');
            if (typeof replaceWith === 'string' || typeof replaceWith === 'number') {
                result.push(possibleText.replace(pattern, `${replaceWith}`));
            }
            else {
                const splits = possibleText.split(pattern);
                splits.forEach((splitText, i) => {
                    if (splitText) {
                        replaceWhiteSpace(splitText).forEach((text) => result.push(text));
                    }
                    // if NOT last
                    if (i + 1 < splits.length) {
                        result.push(replaceWith);
                    }
                });
            }
        }
        else {
            result.push(possibleText);
        }
    });
    return result;
}

function useTranslations() {
    const { values, locale } = React.useContext(translationsContext.TranslationsContext) || {
        locale: 'en-PH',
    };
    return React.useMemo(() => ({
        t(k, option) {
            if (!values) {
                // console.error(
                //   '[useTranslations] t method called before translation values are loaded',
                // )
                return k;
            }
            if (values[k]) {
                return option ? format(values[k] || k, option) : values[k] || k;
            }
            // console.warn(
            //   `[useTranslations] Missing translation for key ${k} and locale ${locale}`,
            // )
            return option ? format(k, option) : k;
        },
        // @ts-ignore
        locale,
    }), [values, locale]);
}

exports.format = format;
exports.useTranslations = useTranslations;
