"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Core = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _services = require("@mirai/services");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _components = require("./components");
var _CookieBanner = require("./components/CookieBanner");
var _helpers = require("./components/helpers");
var _Core = require("./Core.constants");
var _Core2 = require("./Core.Notifications");
var _helpers2 = require("./helpers");
const _excluded = ["cookieBanner", "currency", "locale", "skeleton"],
  _excluded2 = ["el", "component"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const contents = {};
const ghosts = {};
const Core = _ref => {
  let {
    children,
    components = []
  } = _ref;
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const _useStore = (0, _dataSources.useStore)(),
    {
      set,
      value: {
        cookieBanner,
        currency,
        locale = _locale.DEFAULT_LOCALE,
        skeleton = false
      }
    } = _useStore,
    store = _objectWithoutProperties(_useStore.value, _excluded);
  const [dictionary, setDictionary] = (0, _react.useState)(!skeleton ? _services.ServiceDictionary.cache(locale) : undefined);
  (0, _react.useEffect)(() => {
    if (skeleton) return;
    if (!components.some(function () {
      let {
        component
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return Object.values(_Core.COMPONENTS).includes(component);
    })) _dataSources.Event.publish(_helpers.EVENT.CORE_READY, {
      event: _helpers.EVENT.CORE_READY
    });
    fetchDictionary(locale);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  (0, _react.useEffect)(() => {
    if (skeleton) return;
    _dataSources.Event.subscribe(_helpers.EVENT.INTENT_CURRENCY, handleIntentCurrency);
    _dataSources.Event.subscribe(_helpers.EVENT.INTENT_LOCALE, handleIntentLocale);
    _dataSources.Event.subscribe(_helpers.EVENT.LOCALE_CHANGED, handleLocaleChanged);
    return () => {
      _dataSources.Event.unsubscribe(_helpers.EVENT.INTENT_CURRENCY, handleIntentCurrency);
      _dataSources.Event.unsubscribe(_helpers.EVENT.INTENT_LOCALE, handleIntentLocale);
      _dataSources.Event.unsubscribe(_helpers.EVENT.LOCALE_CHANGED, handleLocaleChanged);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [store]);
  const handleIntentCurrency = _ref2 => {
    let {
      currency
    } = _ref2;
    set({
      currency
    });
  };
  const handleIntentLocale = async _ref3 => {
    let {
      locale
    } = _ref3;
    const [language] = locale.split('-');
    set({
      language,
      locale
    });
    (0, _helpers2.themeDirection)(language);
    fetchDictionary(locale);
  };
  const handleLocaleChanged = _ref4 => {
    let {
      locale,
      language
    } = _ref4;
    (0, _helpers2.themeDirection)(language);
    fetchDictionary(locale);
  };
  const fetchDictionary = async function () {
    let locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _locale.DEFAULT_LOCALE;
    const cached = _services.ServiceDictionary.cache(locale);
    if (cached) setDictionary(cached);
    const next = await _services.ServiceDictionary.get(locale, false);
    if (next) setDictionary(next);
    return next;
  };
  return /*#__PURE__*/_react.default.createElement(_locale.LocaleProvider, {
    currency,
    dictionary,
    locale
  }, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !skeleton && cookieBanner && /*#__PURE__*/_react.default.createElement(_CookieBanner.CookieBanner, null), !skeleton && /*#__PURE__*/_react.default.createElement(_Core2.Notifications, null), components.filter(function () {
    let {
      desktop,
      mobile
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return isMobile && (mobile === undefined || mobile === true) && desktop !== true || !isMobile && (desktop === undefined || desktop === true) && mobile !== true;
  }).map(function () {
    let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      {
        el,
        component
      } = _ref5,
      props = _objectWithoutProperties(_ref5, _excluded2);
    let index = arguments.length > 1 ? arguments[1] : undefined;
    if (!ghosts[index]) ghosts[index] = (0, _helpers2.getGhost)(el);
    if (!contents[index]) contents[index] = (0, _helpers2.getContents)(el);
    (0, _helpers2.sanitizeComponent)(el);
    return /*#__PURE__*/_reactDom.default.createPortal(/*#__PURE__*/_react.default.createElement(_components.COMPONENTS[component], _objectSpread(_objectSpread({}, props), {}, {
      dangerousChildren: contents[index],
      el,
      ghost: ghosts[index],
      ['data-testid']: process.env.NODE_ENV === 'production' ? component : undefined
    })), el);
  })), children);
};
exports.Core = Core;
Core.propTypes = {
  children: _propTypes.default.node,
  components: _propTypes.default.arrayOf(_propTypes.default.shape({
    component: _propTypes.default.oneOf(Object.keys(_components.COMPONENTS)),
    el: _propTypes.default.any
  }))
};
//# sourceMappingURL=Core.js.map