"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Notify = void 0;

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

require("./style/index.css");

var _utils = require("../utils");

var _Notify = _interopRequireDefault(require("./Notify"));

var _getContainer = require("../utils/dom/getContainer");

var _lockClick = require("../toast/lock-click");

var _render = require("../utils/dom/render");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const NotifyNamespace = {};

function parseOptions(message) {
  return (0, _utils.isObject)(message) ? message : {
    message
  };
}

const notifyArray = []; // 同步的销毁

function syncClear() {
  let fn = notifyArray.pop();

  while (fn) {
    fn();
    (0, _lockClick.lockClick)(false);
    fn = notifyArray.pop();
  }
} // 针对 toast 还没弹出来就立刻销毁的情况，将销毁放到下一个 event loop 中，避免销毁失败。


function nextTickClear() {
  setTimeout(syncClear);
} // 可返回用于销毁此弹窗的方法


const show = p => {
  const props = parseOptions(p);
  const interProps = Object.assign(Object.assign({}, NotifyNamespace.currentOptions), props);
  const {
    onClose = _utils.noop,
    duration
  } = interProps,
        restProps = (0, _tslib().__rest)(interProps, ["onClose", "duration"]);
  let timer = 0;
  const userContainer = (0, _getContainer.resolveContainer)(props.teleport);
  const container = document.createElement('div');
  userContainer.appendChild(container);
  let destroy = _utils.noop;

  const TempNotify = () => {
    const [visible, setVisible] = (0, _react().useState)(false);

    destroy = () => {
      setVisible(false);
      if (onClose) onClose();
    }; // clearDOM after animation


    const internalOnClosed = () => {
      const unmountResult = (0, _render.unmount)(container);

      if (unmountResult && container.parentNode) {
        container.parentNode.removeChild(container);
      }
    };

    const internalAfterClose = (0, _utils.once)(() => {
      internalOnClosed();
    });
    (0, _react().useEffect)(() => {
      setVisible(true);
      syncClear();
      notifyArray.push(internalAfterClose);

      if (duration && +duration > 0) {
        timer = window.setTimeout(destroy, duration);
      }

      return () => {
        if (timer !== 0) {
          window.clearTimeout(timer);
        }
      }; // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    return (0, _jsxRuntime().jsx)(_Notify.default, Object.assign({}, restProps, {
      visible: visible,
      teleport: () => container,
      onClose: onClose,
      onClosed: internalOnClosed
    }));
  };

  (0, _render.render)((0, _jsxRuntime().jsx)(TempNotify, {}), container);
  return destroy;
};

function defaultOptions() {
  return {
    type: 'danger',
    color: undefined,
    message: '',
    onClose: undefined,
    onClick: undefined,
    duration: 3000,
    className: '',
    lockScroll: false,
    background: undefined
  };
}

NotifyNamespace.currentOptions = defaultOptions();

const setDefaultOptions = options => {
  (0, _utils.extend)(NotifyNamespace.currentOptions, options);
};

const resetDefaultOptions = () => {
  NotifyNamespace.currentOptions = defaultOptions();
};

const clear = nextTickClear;
const exportNotifyNamespace = Object.assign(_Notify.default, {
  show,
  setDefaultOptions,
  resetDefaultOptions,
  clear
});
exports.Notify = exportNotifyNamespace;