"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 }; }
const DELAY_WELCOME = 10000;
let focused = false;
const ButtonDispatcher = _ref => {
  let {
    settings,
    visible,
    onPress
  } = _ref;
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      session: {
        firstName
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const {
    ai: {
      name = _Chat.DEFAULT_NAME
    } = {},
    web: {
      avatar
    } = {}
  } = settings || {};
  const [focus, setFocus] = (0, _react.useState)(false);
  const [hide, setHide] = (0, _react.useState)(false);
  const [ready, setReady] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    const callback = _ref2 => {
      let {
        visible: modalVisible
      } = _ref2;
      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