"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TOAST_ROOT_CLASS_NAME = exports.FcrToastTransitionGroup = exports.FcrToastApiFactory = exports.FcrToastApi = exports.FcrToast = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
require("core-js/modules/es.array.push.js");
require("core-js/modules/web.timers.js");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _reactDom = _interopRequireWildcard(require("react-dom"));
var _reactTransitionGroup = require("react-transition-group");
var _uuid = require("uuid");
require("./index.css");
var _type = require("../icon/type");
var _icon = require("../icon");
var _safelyRemoveChild = require("agora-foundation/lib/utilities/safely-remove-child");
var _jsxRuntime = require("react/jsx-runtime");
var _FcrToastApi;
var _excluded = ["onEnded", "duration", "position"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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 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) { (0, _defineProperty2["default"])(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; }
var FcrToast = exports.FcrToast = function FcrToast(props) {
  var content = props.content,
    icon = props.icon,
    _props$type = props.type,
    type = _props$type === void 0 ? 'normal' : _props$type,
    closable = props.closable,
    action = props.action,
    onClose = props.onClose,
    _props$size = props.size,
    size = _props$size === void 0 ? 'large' : _props$size;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    style: {
      paddingRight: closable ? '0' : '16px',
      paddingLeft: '16px'
    },
    className: (0, _classnames["default"])('fcr-toast-container', "fcr-toast-".concat(size), "fcr-toast-".concat(type.toLowerCase())),
    children: [icon && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames["default"])('fcr-toast-container-icon'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
        type: icon,
        colors: {
          iconPrimary: type === 'normal' || type === 'warn' ? 'var(--fcr_ui_scene_black10)' : 'var(--fcr_ui_scene_white10)'
        },
        size: 24
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: {
        paddingRight: closable || action ? '10px' : '0'
      },
      className: (0, _classnames["default"])('fcr-toast-container-content'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: content
      })
    }), action && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames["default"])('fcr-toast-container-action', 'fcr-divider'),
      onClick: action.onClick,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: action.text
      })
    }), closable && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      onClick: onClose,
      className: (0, _classnames["default"])('fcr-toast-container-close', 'fcr-divider'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
        type: _type.FcrIconType.FCR_CLOSE,
        colors: {
          iconPrimary: type === 'normal' || type === 'warn' ? 'var(--fcr_ui_scene_black10)' : 'var(--fcr_ui_scene_white10)'
        },
        size: 20
      })
    })]
  });
};
var FcrToastTransitionGroup = exports.FcrToastTransitionGroup = function FcrToastTransitionGroup(props) {
  var onEnded = props.onEnded,
    _props$duration = props.duration,
    duration = _props$duration === void 0 ? 3000 : _props$duration,
    _props$position = props.position,
    position = _props$position === void 0 ? 'top' : _props$position,
    others = (0, _objectWithoutProperties2["default"])(props, _excluded);
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    active = _useState2[0],
    setActive = _useState2[1];
  var timerRef = (0, _react.useRef)(-1);
  (0, _react.useEffect)(function () {
    setActive(true);
    timerRef.current = window.setTimeout(function () {
      setActive(false);
    }, duration);
    return function () {
      window.clearTimeout(timerRef.current);
    };
  }, []);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactTransitionGroup.CSSTransition, {
    onExited: onEnded,
    mountOnEnter: true,
    unmountOnExit: true,
    classNames: 'fcr-toast-anim',
    "in": active,
    timeout: 200,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: {
        width: 'fit-content'
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(FcrToast, _objectSpread(_objectSpread({}, others), {}, {
        onClose: function onClose() {
          var _others$onClose;
          setActive(false);
          (_others$onClose = others.onClose) === null || _others$onClose === void 0 || _others$onClose.call(others);
        }
      }))
    })
  });
};
var TOAST_ROOT_CLASS_NAME = exports.TOAST_ROOT_CLASS_NAME = 'fcr-toast-root';
var FcrToastApi = exports.FcrToastApi = /*#__PURE__*/function () {
  function FcrToastApi() {
    (0, _classCallCheck2["default"])(this, FcrToastApi);
  }
  return (0, _createClass2["default"])(FcrToastApi, null, [{
    key: "_presistToastContainer",
    get: function get() {
      return document.querySelector(".".concat(this._presistToastContainerCls));
    }
  }, {
    key: "_singleToastContainer",
    get: function get() {
      return document.querySelector(".".concat(this._singleToastContainerCls));
    }
  }, {
    key: "_toastRoot",
    get: function get() {
      var _document$querySelect, _this$customRootClass;
      return (_document$querySelect = document.querySelector(".".concat((_this$customRootClass = this.customRootClass) !== null && _this$customRootClass !== void 0 ? _this$customRootClass : TOAST_ROOT_CLASS_NAME))) !== null && _document$querySelect !== void 0 ? _document$querySelect : document.querySelector(".".concat(TOAST_ROOT_CLASS_NAME));
    }
  }, {
    key: "open",
    value: function open(config) {
      var _this = this;
      this.customRootClass = config.customRootClass;
      if (!config.id) config.id = (0, _uuid.v4)();
      if (config.persist) {
        this._presistToasts.push(config);
        this._scanRenderableToast();
      } else {
        if (this._singleToast) {
          this._unmountToast(this._singleToast);
          this._singleToast = null;
        }
        this._singleToast = config;
        this._render(this._singleToast, function () {
          _this._singleToast = null;
        });
      }
    }
  }, {
    key: "destroyAll",
    value: function destroyAll() {
      if (this._singleToast) {
        if (this._singleToastContainer) {
          (0, _safelyRemoveChild.safelyRemoveChild)(this._toastRoot, this._singleToastContainer);
        }
        this._unmountToast(this._singleToast);
        this._singleToast = null;
      }
      this._presistToasts = [];
      if (this._presistToastContainer) {
        (0, _safelyRemoveChild.safelyRemoveChild)(this._toastRoot, this._presistToastContainer);
      }
    }
  }]);
}();
_FcrToastApi = FcrToastApi;
(0, _defineProperty2["default"])(FcrToastApi, "customRootClass", undefined);
(0, _defineProperty2["default"])(FcrToastApi, "_maxPresistToastRenderCount", 2);
(0, _defineProperty2["default"])(FcrToastApi, "_presistToastRenderCount", 0);
(0, _defineProperty2["default"])(FcrToastApi, "_presistToasts", []);
(0, _defineProperty2["default"])(FcrToastApi, "_singleToast", null);
(0, _defineProperty2["default"])(FcrToastApi, "_singleToastContainerCls", 'fcr-single-toast-container');
(0, _defineProperty2["default"])(FcrToastApi, "_presistToastContainerCls", 'fcr-presist-toast-container');
(0, _defineProperty2["default"])(FcrToastApi, "_unmountToast", function (toast) {
  if (toast.portal) {
    (0, _reactDom.unmountComponentAtNode)(toast.portal);
    var container = toast.persist ? _FcrToastApi._presistToastContainer : _FcrToastApi._singleToastContainer;
    (0, _safelyRemoveChild.safelyRemoveChild)(container, toast.portal);
    if (toast.persist) {
      _FcrToastApi._presistToastRenderCount--;
    }
    _FcrToastApi._removeToastContainer();
  }
});
(0, _defineProperty2["default"])(FcrToastApi, "_preCheck", function () {
  return _FcrToastApi._presistToastRenderCount < _FcrToastApi._maxPresistToastRenderCount;
});
(0, _defineProperty2["default"])(FcrToastApi, "_createToastContainer", function (persist) {
  if (persist) {
    if (!_FcrToastApi._presistToastContainer) {
      var _FcrToastApi$_toastRo;
      var presistToastContainer = document.createElement('div');
      presistToastContainer.className = _FcrToastApi._presistToastContainerCls;
      (_FcrToastApi$_toastRo = _FcrToastApi._toastRoot) === null || _FcrToastApi$_toastRo === void 0 || _FcrToastApi$_toastRo.appendChild(presistToastContainer);
    }
  } else {
    if (!_FcrToastApi._singleToastContainer) {
      var _FcrToastApi$_toastRo2;
      var singleToastContainer = document.createElement('div');
      singleToastContainer.className = _FcrToastApi._singleToastContainerCls;
      (_FcrToastApi$_toastRo2 = _FcrToastApi._toastRoot) === null || _FcrToastApi$_toastRo2 === void 0 || _FcrToastApi$_toastRo2.appendChild(singleToastContainer);
    }
  }
});
(0, _defineProperty2["default"])(FcrToastApi, "_removeToastContainer", function () {
  if (_FcrToastApi._presistToastRenderCount === 0) {
    if (_FcrToastApi._presistToastContainer) {
      (0, _safelyRemoveChild.safelyRemoveChild)(_FcrToastApi._toastRoot, _FcrToastApi._presistToastContainer);
    }
  }
  if (!_FcrToastApi._singleToast) {
    if (_FcrToastApi._singleToastContainer) {
      (0, _safelyRemoveChild.safelyRemoveChild)(_FcrToastApi._toastRoot, _FcrToastApi._singleToastContainer);
    }
  }
});
(0, _defineProperty2["default"])(FcrToastApi, "_scanRenderableToast", function () {
  if (!_FcrToastApi._preCheck()) return;
  var renderableToast = _FcrToastApi._presistToasts.shift();
  if (renderableToast) {
    _FcrToastApi._presistToastRenderCount++;
    _FcrToastApi._render(renderableToast, function () {
      _FcrToastApi._scanRenderableToast();
    });
  }
});
(0, _defineProperty2["default"])(FcrToastApi, "_render", function (toast, onClose) {
  _FcrToastApi._createToastContainer(!!toast.persist);
  var container = toast.persist ? _FcrToastApi._presistToastContainer : _FcrToastApi._singleToastContainer;
  toast.portal = document.createElement('div');
  toast.portal.className = 'fcr-toast-portal';
  container === null || container === void 0 || container.appendChild(toast.portal);
  _reactDom["default"].render(/*#__PURE__*/(0, _jsxRuntime.jsx)(FcrToastTransitionGroup, _objectSpread({
    onEnded: function onEnded() {
      if (toast) {
        _FcrToastApi._unmountToast(toast);
        onClose === null || onClose === void 0 || onClose();
      }
    },
    duration: toast.duration
  }, toast.toastProps)), toast.portal);
});
var FcrToastApiFactory = exports.FcrToastApiFactory = /*#__PURE__*/function () {
  function FcrToastApiFactory(_ref) {
    var _this2 = this;
    var toastPlacement = _ref.toastPlacement,
      renderContainer = _ref.renderContainer;
    (0, _classCallCheck2["default"])(this, FcrToastApiFactory);
    (0, _defineProperty2["default"])(this, "_renderContainer", null);
    (0, _defineProperty2["default"])(this, "_toast", null);
    (0, _defineProperty2["default"])(this, "_unmountToast", function (toast) {
      if (toast.portal) {
        (0, _reactDom.unmountComponentAtNode)(toast.portal);
        var container = _this2._toastContainer;
        if (container) {
          (0, _safelyRemoveChild.safelyRemoveChild)(container.getRootNode(), toast.portal);
        }
        _this2._removeToastContainer();
      }
    });
    (0, _defineProperty2["default"])(this, "_createToastContainer", function () {
      if (!_this2._toastContainer && _this2._renderContainer) {
        var singleToastContainer = document.createElement('div');
        singleToastContainer.className = _this2._toastContainerCls;
        _this2._renderContainer.appendChild(singleToastContainer);
      }
    });
    (0, _defineProperty2["default"])(this, "_removeToastContainer", function () {
      if (!_this2._toast) {
        if (_this2._toastContainer && _this2._renderContainer) {
          (0, _safelyRemoveChild.safelyRemoveChild)(_this2._renderContainer, _this2._toastContainer);
        }
      }
    });
    (0, _defineProperty2["default"])(this, "_render", function (toast, onClose) {
      _this2._createToastContainer();
      var container = _this2._toastContainer;
      toast.portal = document.createElement('div');
      toast.portal.className = 'fcr-toast-portal';
      container === null || container === void 0 || container.appendChild(toast.portal);
      _reactDom["default"].render(/*#__PURE__*/(0, _jsxRuntime.jsx)(FcrToastTransitionGroup, _objectSpread({
        onEnded: function onEnded() {
          if (toast) {
            _this2._unmountToast(toast);
            onClose === null || onClose === void 0 || onClose();
          }
        },
        duration: toast.duration
      }, toast.toastProps)), toast.portal);
    });
    this._toastPlacement = toastPlacement;
    this._renderContainer = renderContainer;
  }
  return (0, _createClass2["default"])(FcrToastApiFactory, [{
    key: "_toastContainer",
    get: function get() {
      var _this$_renderContaine;
      return (_this$_renderContaine = this._renderContainer) === null || _this$_renderContaine === void 0 ? void 0 : _this$_renderContaine.querySelector(".".concat(this._toastContainerCls));
    }
  }, {
    key: "_toastContainerCls",
    get: function get() {
      return "fcr-toast-placement-".concat(this._toastPlacement);
    }
  }, {
    key: "open",
    value: function open(config) {
      var _this3 = this;
      if (!config.id) config.id = (0, _uuid.v4)();
      if (this._toast) {
        this._unmountToast(this._toast);
        this._toast = null;
      }
      this._toast = config;
      this._render(this._toast, function () {
        _this3._toast = null;
      });
    }
  }]);
}();