"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ButtonDispatcher = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _ButtonDispatcher = require("./ButtonDispatcher.l10n");
var style = _interopRequireWildcard(require("./ButtonDispatcher.module.css"));
var _helpers = require("./helpers");
var _helpers2 = require("../../../helpers");
var _Chat = require("../../Chat.constants");
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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
const DELAY_WELCOME = 10000;
let focused = false;
const ButtonDispatcher = _ref => {
  let settings = _ref.settings,
    visible = _ref.visible,
    onPress = _ref.onPress;
  const _useDevice = (0, _ui.useDevice)(),
    isMobile = _useDevice.isMobile;
  const _useLocale = (0, _locale.useLocale)(),
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    _useStore$value2$sess = _useStore$value2.session,
    _useStore$value2$sess2 = _useStore$value2$sess === void 0 ? {} : _useStore$value2$sess,
    firstName = _useStore$value2$sess2.firstName;
  const _ref2 = settings || {},
    _ref2$ai = _ref2.ai,
    _ref2$ai2 = _ref2$ai === void 0 ? {} : _ref2$ai,
    _ref2$ai2$name = _ref2$ai2.name,
    name = _ref2$ai2$name === void 0 ? _Chat.DEFAULT_NAME : _ref2$ai2$name,
    _ref2$web = _ref2.web,
    _ref2$web2 = _ref2$web === void 0 ? {} : _ref2$web,
    avatar = _ref2$web2.avatar;
  const _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    focus = _useState2[0],
    setFocus = _useState2[1];
  const _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    hide = _useState4[0],
    setHide = _useState4[1];
  const _useState5 = (0, _react.useState)(false),
    _useState6 = _slicedToArray(_useState5, 2),
    ready = _useState6[0],
    setReady = _useState6[1];
  (0, _react.useEffect)(() => {
    const callback = _ref3 => {
      let modalVisible = _ref3.visible;
      setHide(modalVisible);
    };
    setTimeout(() => setReady(true), 500);
    _dataSources.Event.subscribe(_helpers2.EVENT.MODAL_VISIBLE, callback);
    return () => _dataSources.Event.unsubscribe(_helpers2.EVENT.MODAL_VISIBLE, callback);
  }, []);
  (0, _react.useEffect)(() => {
    if (!ready || focused) return;
    let inactivityTimer;
    const resetTimer = () => {
      clearTimeout(inactivityTimer);
      if (focused) return;
      inactivityTimer = setTimeout(() => {
        setFocus(true);
        clearTimeout(inactivityTimer);
      }, DELAY_WELCOME);
    };
    document.addEventListener('mousemove', resetTimer);
    document.addEventListener('scroll', resetTimer);
    resetTimer();
    return () => {
      clearTimeout(inactivityTimer.current);
      document.removeEventListener('mousemove', resetTimer);
      document.removeEventListener('scroll', resetTimer);
    };
  }, [ready]);
  (0, _react.useEffect)(() => {
    if (focus) focused = true;
  }, [focus]);
  const position = (0, _helpers.determinePosition)(settings === null || settings === void 0 ? void 0 : settings.web, isMobile);
  return /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.buttonDispatcher, ready && !visible && !hide && style.visible, ...position.className),
    style: position.style
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: (0, _ui.styles)(style.welcome, focus && !visible && style.visible)
  }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    brand: true,
    headline: true,
    className: style.hi
  }, "\u2726 ".concat(translate(_ButtonDispatcher.L10N.LABEL_HI)).concat(firstName ? ", ".concat(firstName) : '')), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    markdown: false,
    medium: true
  }, translate(_ButtonDispatcher.L10N.LABEL_GREETINGS, {
    name
  }))), isMobile && /*#__PURE__*/_react.default.createElement(_ui.Button, {
    icon: "close",
    small: true,
    squared: true,
    transparent: true,
    onPress: () => setFocus(false)
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers2.ICON.CLOSE
  }))), /*#__PURE__*/_react.default.createElement(_ui.Pressable, {
    onEnter: () => setFocus(true),
    onLeave: () => setFocus(false),
    onPress: onPress
  }, /*#__PURE__*/_react.default.createElement(_ui.Avatar, {
    disabled: visible,
    image: avatar,
    className: style.avatar
  })));
};
exports.ButtonDispatcher = ButtonDispatcher;
ButtonDispatcher.displayName = 'Mirai:Core:Chat:ButtonDispatcher';
ButtonDispatcher.propTypes = {
  settings: _propTypes.default.shape({
    ai: _propTypes.default.shape({
      name: _propTypes.default.string
    }),
    web: _propTypes.default.shape({
      avatar: _propTypes.default.string,
      bottom: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      bottomMobile: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      left: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      leftMobile: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      right: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      rightMobile: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      top: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number]),
      topMobile: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.number])
    })
  }),
  visible: _propTypes.default.bool,
  onPress: _propTypes.default.func
};
//# sourceMappingURL=ButtonDispatcher.js.map