"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Mask = void 0;
var _nativeProps = require("../../utils/native-props");
var _react = _interopRequireWildcard(require("react"));
var _ahooks = require("ahooks");
var _useLockScroll = require("../../utils/use-lock-scroll");
var _web = require("@react-spring/web");
var _renderToContainer = require("../../utils/render-to-container");
var _withDefaultProps = require("../../utils/with-default-props");
var _configProvider = require("../config-provider");
var _shouldRender = require("../../utils/should-render");
var _withStopPropagation = require("../../utils/with-stop-propagation");
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); }
const classPrefix = `adm-mask`;
const opacityRecord = {
  default: 0.55,
  thin: 0.35,
  thick: 0.75
};
const colorRecord = {
  black: '0, 0, 0',
  white: '255, 255, 255'
};
const defaultProps = {
  visible: true,
  destroyOnClose: false,
  forceRender: false,
  color: 'black',
  opacity: 'default',
  disableBodyScroll: true,
  getContainer: null,
  stopPropagation: ['click']
};
const Mask = p => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const {
    locale
  } = (0, _configProvider.useConfig)();
  const ref = (0, _react.useRef)(null);
  (0, _useLockScroll.useLockScroll)(ref, props.visible && props.disableBodyScroll);
  const background = (0, _react.useMemo)(() => {
    var _a;
    const opacity = (_a = opacityRecord[props.opacity]) !== null && _a !== void 0 ? _a : props.opacity;
    const rgb = colorRecord[props.color];
    return rgb ? `rgba(${rgb}, ${opacity})` : props.color;
  }, [props.color, props.opacity]);
  const [active, setActive] = (0, _react.useState)(props.visible);
  const unmountedRef = (0, _ahooks.useUnmountedRef)();
  const {
    opacity
  } = (0, _web.useSpring)({
    opacity: props.visible ? 1 : 0,
    config: {
      precision: 0.01,
      mass: 1,
      tension: 250,
      friction: 30,
      clamp: true
    },
    onStart: () => {
      setActive(true);
    },
    onRest: () => {
      var _a, _b;
      if (unmountedRef.current) return;
      setActive(props.visible);
      if (props.visible) {
        (_a = props.afterShow) === null || _a === void 0 ? void 0 : _a.call(props);
      } else {
        (_b = props.afterClose) === null || _b === void 0 ? void 0 : _b.call(props);
      }
    }
  });
  const node = (0, _withStopPropagation.withStopPropagation)(props.stopPropagation, (0, _nativeProps.withNativeProps)(props, _react.default.createElement(_web.animated.div, {
    className: classPrefix,
    ref: ref,
    "aria-hidden": true,
    style: Object.assign(Object.assign({}, props.style), {
      background,
      opacity,
      display: active ? undefined : 'none'
    }),
    onClick: e => {
      var _a;
      if (e.target === e.currentTarget) {
        (_a = props.onMaskClick) === null || _a === void 0 ? void 0 : _a.call(props, e);
      }
    }
  }, props.onMaskClick && _react.default.createElement("div", {
    className: `${classPrefix}-aria-button`,
    role: 'button',
    "aria-label": locale.Mask.name,
    onClick: props.onMaskClick
  }), _react.default.createElement("div", {
    className: `${classPrefix}-content`
  }, props.children))));
  return _react.default.createElement(_shouldRender.ShouldRender, {
    active: active,
    forceRender: props.forceRender,
    destroyOnClose: props.destroyOnClose
  }, (0, _renderToContainer.renderToContainer)(props.getContainer, node));
};
exports.Mask = Mask;