"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _Button = _interopRequireDefault(require("../Button"));
var _ModalWrap = require("./ModalWrap");
var _usePortal = _interopRequireDefault(require("../..\\utils\\usePortal"));
var _uuid = require("../..\\utils\\uuid");
var _useClickOutSide = require("../..\\utils\\useClickOutSide");
var _jsxRuntime = require("react/jsx-runtime");
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const Modal = ({
  children,
  onCancel,
  onOk,
  id: _id = (0, _uuid.uuid)(),
  controlVisible: _controlVisible = false,
  showCloseButton: _showCloseButton = false,
  canClickOutsideToClose: _canClickOutsideToClose = true,
  hiddenScroll: _hiddenScroll = true,
  portal: _portal = false,
  parentId: _parentId = 'root',
  trigger,
  mask: _mask = false
}) => {
  const [visible, setVisible] = (0, _react.useState)(_controlVisible);
  const modalRef = (0, _react.useRef)(null);
  const triggerDom = (0, _react.useMemo)(() => {
    if (!trigger) {
      return null;
    }
    return /*#__PURE__*/_react.default.cloneElement(trigger, _objectSpread(_objectSpread({
      key: 'trigger'
    }, trigger.props), {}, {
      onClick: async e => {
        var _trigger$props, _trigger$props$onClic;
        setVisible(!visible);
        (_trigger$props = trigger.props) === null || _trigger$props === void 0 ? void 0 : (_trigger$props$onClic = _trigger$props.onClick) === null || _trigger$props$onClic === void 0 ? void 0 : _trigger$props$onClic.call(_trigger$props, e);
      }
    }));
  }, [setVisible, trigger, visible]);
  const handleClose = () => {
    setVisible(false);
  };
  (0, _useClickOutSide.useClickOutside)(modalRef, () => {
    if (_canClickOutsideToClose) {
      handleClose();
    }
  });
  (0, _react.useEffect)(() => {
    if (visible && _hiddenScroll) {
      document.body.style.overflow = 'hidden';
      document.body.style.paddingRight = '15px';
    } else {
      document.body.style.overflow = 'auto';
      document.body.style.paddingRight = '0px';
    }
  }, [visible, _id]);
  const target = (0, _usePortal.default)(_parentId);
  const elements = /*#__PURE__*/(0, _jsxRuntime.jsx)(_ModalWrap.ModalWrap, {
    visible: visible,
    mask: _mask,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      id: _id,
      className: "maskContainer",
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "modalContainer",
        ref: modalRef,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "headerContainer",
          children: _showCloseButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            onClick: () => {
              handleClose();
            },
            width: 20,
            type: 'text',
            label: 'X'
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          onClick: e => {
            e.stopPropagation();
          },
          className: "childrenContainer",
          children: children
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "footerContainer",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            label: "cancel",
            onClick: () => {
              handleClose();
              onCancel === null || onCancel === void 0 ? void 0 : onCancel();
            },
            style: {
              marginRight: 10
            }
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            label: "ok",
            type: "primary",
            onClick: () => {
              handleClose();
              onOk === null || onOk === void 0 ? void 0 : onOk();
            }
          })]
        })]
      })
    })
  });
  return _portal ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [triggerDom, /*#__PURE__*/(0, _reactDom.createPortal)(elements, target)]
  }) : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [triggerDom, elements]
  });
};
var _default = Modal;
exports.default = _default;