import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _FcrToastApi;
var _excluded = ["onEnded", "duration", "position"];
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; }
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.object.define-properties.js";
import "core-js/modules/es.object.define-property.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.filter.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import "core-js/modules/web.timers.js";
import classNames from 'classnames';
import { useEffect, useRef, useState } from 'react';
import ReactDOM, { unmountComponentAtNode } from 'react-dom';
import { CSSTransition } from 'react-transition-group';
import { v4 as uuidv4 } from 'uuid';
import './index.css';
import { FcrIconType } from '../icon/type';
import { FcrIcon } from '../icon';
import { safelyRemoveChild } from 'agora-foundation/lib/utilities/safely-remove-child';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var 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__*/_jsxs("div", {
    style: {
      paddingRight: closable ? '0' : '16px',
      paddingLeft: '16px'
    },
    className: classNames('fcr-toast-container', "fcr-toast-".concat(size), "fcr-toast-".concat(type.toLowerCase())),
    children: [icon && /*#__PURE__*/_jsx("div", {
      className: classNames('fcr-toast-container-icon'),
      children: /*#__PURE__*/_jsx(FcrIcon, {
        type: icon,
        colors: {
          iconPrimary: type === 'normal' || type === 'warn' ? 'var(--fcr_ui_scene_black10)' : 'var(--fcr_ui_scene_white10)'
        },
        size: 24
      })
    }), /*#__PURE__*/_jsx("div", {
      style: {
        paddingRight: closable || action ? '10px' : '0'
      },
      className: classNames('fcr-toast-container-content'),
      children: /*#__PURE__*/_jsx("span", {
        children: content
      })
    }), action && /*#__PURE__*/_jsx("div", {
      className: classNames('fcr-toast-container-action', 'fcr-divider'),
      onClick: action.onClick,
      children: /*#__PURE__*/_jsx("span", {
        children: action.text
      })
    }), closable && /*#__PURE__*/_jsx("div", {
      onClick: onClose,
      className: classNames('fcr-toast-container-close', 'fcr-divider'),
      children: /*#__PURE__*/_jsx(FcrIcon, {
        type: FcrIconType.FCR_CLOSE,
        colors: {
          iconPrimary: type === 'normal' || type === 'warn' ? 'var(--fcr_ui_scene_black10)' : 'var(--fcr_ui_scene_white10)'
        },
        size: 20
      })
    })]
  });
};
export var 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 = _objectWithoutProperties(props, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    active = _useState2[0],
    setActive = _useState2[1];
  var timerRef = useRef(-1);
  useEffect(function () {
    setActive(true);
    timerRef.current = window.setTimeout(function () {
      setActive(false);
    }, duration);
    return function () {
      window.clearTimeout(timerRef.current);
    };
  }, []);
  return /*#__PURE__*/_jsx(CSSTransition, {
    onExited: onEnded,
    mountOnEnter: true,
    unmountOnExit: true,
    classNames: 'fcr-toast-anim',
    "in": active,
    timeout: 200,
    children: /*#__PURE__*/_jsx("div", {
      style: {
        width: 'fit-content'
      },
      children: /*#__PURE__*/_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);
        }
      }))
    })
  });
};
export var TOAST_ROOT_CLASS_NAME = 'fcr-toast-root';
export var FcrToastApi = /*#__PURE__*/function () {
  function FcrToastApi() {
    _classCallCheck(this, FcrToastApi);
  }
  return _createClass(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 = uuidv4();
      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) {
          safelyRemoveChild(this._toastRoot, this._singleToastContainer);
        }
        this._unmountToast(this._singleToast);
        this._singleToast = null;
      }
      this._presistToasts = [];
      if (this._presistToastContainer) {
        safelyRemoveChild(this._toastRoot, this._presistToastContainer);
      }
    }
  }]);
}();
_FcrToastApi = FcrToastApi;
_defineProperty(FcrToastApi, "customRootClass", undefined);
_defineProperty(FcrToastApi, "_maxPresistToastRenderCount", 2);
_defineProperty(FcrToastApi, "_presistToastRenderCount", 0);
_defineProperty(FcrToastApi, "_presistToasts", []);
_defineProperty(FcrToastApi, "_singleToast", null);
_defineProperty(FcrToastApi, "_singleToastContainerCls", 'fcr-single-toast-container');
_defineProperty(FcrToastApi, "_presistToastContainerCls", 'fcr-presist-toast-container');
_defineProperty(FcrToastApi, "_unmountToast", function (toast) {
  if (toast.portal) {
    unmountComponentAtNode(toast.portal);
    var container = toast.persist ? _FcrToastApi._presistToastContainer : _FcrToastApi._singleToastContainer;
    safelyRemoveChild(container, toast.portal);
    if (toast.persist) {
      _FcrToastApi._presistToastRenderCount--;
    }
    _FcrToastApi._removeToastContainer();
  }
});
_defineProperty(FcrToastApi, "_preCheck", function () {
  return _FcrToastApi._presistToastRenderCount < _FcrToastApi._maxPresistToastRenderCount;
});
_defineProperty(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);
    }
  }
});
_defineProperty(FcrToastApi, "_removeToastContainer", function () {
  if (_FcrToastApi._presistToastRenderCount === 0) {
    if (_FcrToastApi._presistToastContainer) {
      safelyRemoveChild(_FcrToastApi._toastRoot, _FcrToastApi._presistToastContainer);
    }
  }
  if (!_FcrToastApi._singleToast) {
    if (_FcrToastApi._singleToastContainer) {
      safelyRemoveChild(_FcrToastApi._toastRoot, _FcrToastApi._singleToastContainer);
    }
  }
});
_defineProperty(FcrToastApi, "_scanRenderableToast", function () {
  if (!_FcrToastApi._preCheck()) return;
  var renderableToast = _FcrToastApi._presistToasts.shift();
  if (renderableToast) {
    _FcrToastApi._presistToastRenderCount++;
    _FcrToastApi._render(renderableToast, function () {
      _FcrToastApi._scanRenderableToast();
    });
  }
});
_defineProperty(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.render(/*#__PURE__*/_jsx(FcrToastTransitionGroup, _objectSpread({
    onEnded: function onEnded() {
      if (toast) {
        _FcrToastApi._unmountToast(toast);
        onClose === null || onClose === void 0 || onClose();
      }
    },
    duration: toast.duration
  }, toast.toastProps)), toast.portal);
});
export var FcrToastApiFactory = /*#__PURE__*/function () {
  function FcrToastApiFactory(_ref) {
    var _this2 = this;
    var toastPlacement = _ref.toastPlacement,
      renderContainer = _ref.renderContainer;
    _classCallCheck(this, FcrToastApiFactory);
    _defineProperty(this, "_renderContainer", null);
    _defineProperty(this, "_toast", null);
    _defineProperty(this, "_unmountToast", function (toast) {
      if (toast.portal) {
        unmountComponentAtNode(toast.portal);
        var container = _this2._toastContainer;
        if (container) {
          safelyRemoveChild(container.getRootNode(), toast.portal);
        }
        _this2._removeToastContainer();
      }
    });
    _defineProperty(this, "_createToastContainer", function () {
      if (!_this2._toastContainer && _this2._renderContainer) {
        var singleToastContainer = document.createElement('div');
        singleToastContainer.className = _this2._toastContainerCls;
        _this2._renderContainer.appendChild(singleToastContainer);
      }
    });
    _defineProperty(this, "_removeToastContainer", function () {
      if (!_this2._toast) {
        if (_this2._toastContainer && _this2._renderContainer) {
          safelyRemoveChild(_this2._renderContainer, _this2._toastContainer);
        }
      }
    });
    _defineProperty(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.render(/*#__PURE__*/_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 _createClass(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 = uuidv4();
      if (this._toast) {
        this._unmountToast(this._toast);
        this._toast = null;
      }
      this._toast = config;
      this._render(this._toast, function () {
        _this3._toast = null;
      });
    }
  }]);
}();