"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

var _react = _interopRequireWildcard(require("react"));

var _rcDrawer = _interopRequireDefault(require("rc-drawer"));

var _classnames = _interopRequireDefault(require("classnames"));

var _headerAction = _interopRequireDefault(require("./headerAction"));

var _useForceUpdate = _interopRequireDefault(require("./utils/useForceUpdate"));

require("rc-drawer/assets/index.css");

var _getLocale = require("../../utils/getLocale");

require("./index.css");

var _context = _interopRequireDefault(require("../drawer-manager/context"));

var _dom = require("./utils/dom");

var _excluded = ["visible", "placement", "children", "mask", "width", "className", "contentWrapperStyle", "onClose", "style", "afterVisibleChange", "destroyOnClose", "bodyStyle", "zIndex", "autoClosable"];

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 _getRequireWildcardCache(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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var DrawerContext = /*#__PURE__*/_react["default"].createContext(null);

var Drawer = function Drawer(props) {
  var rcRef = (0, _react.useRef)();
  var destroy = (0, _react.useRef)(false);
  var forceUpdate = (0, _useForceUpdate["default"])();

  var visible = props.visible,
      _props$placement = props.placement,
      placement = _props$placement === void 0 ? 'right' : _props$placement,
      children = props.children,
      _props$mask = props.mask,
      mask = _props$mask === void 0 ? false : _props$mask,
      _props$width = props.width,
      width = _props$width === void 0 ? 820 : _props$width,
      className = props.className,
      contentWrapperStyle = props.contentWrapperStyle,
      onClose = props.onClose,
      style = props.style,
      afterVisibleChange = props.afterVisibleChange,
      _props$destroyOnClose = props.destroyOnClose,
      destroyOnClose = _props$destroyOnClose === void 0 ? true : _props$destroyOnClose,
      bodyStyle = props.bodyStyle,
      _props$zIndex = props.zIndex,
      zIndex = _props$zIndex === void 0 ? 1200 : _props$zIndex,
      _props$autoClosable = props.autoClosable,
      autoClosable = _props$autoClosable === void 0 ? false : _props$autoClosable,
      rest = _objectWithoutProperties(props, _excluded);

  var handleAfterVisibleChange = (0, _react.useCallback)(function (opened) {
    afterVisibleChange && afterVisibleChange(opened);

    if (!opened) {
      if (destroyOnClose) {
        destroy.current = true;
        forceUpdate();
      }
    }
  }, []);

  function renderContent() {
    if (destroy.current && !visible) return null;
    destroy.current = false;
    return children;
  }

  var autoClose = function autoClose(e) {
    if ((0, _dom.hasAncestorElement)(e.target, 'document-mouse-event-ignore,rc-tooltip,titaui-popup,rc-dialog-root,common-modal,in-modal,task-hot-add,addClassName')) {
      return;
    }

    onClose();
  };

  (0, _react.useEffect)(function () {
    if (autoClosable) {
      document.addEventListener('mousedown', autoClose);
    }

    return function () {
      document.removeEventListener('mousedown', autoClose);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement(DrawerContext.Provider, {
    value: {
      close: function close() {
        onClose();
      }
    }
  }, /*#__PURE__*/_react["default"].createElement(_rcDrawer["default"], _extends({
    className: (0, _classnames["default"])('titaui-drawer', 'document-mouse-event-ignore', className),
    style: _objectSpread(_objectSpread({}, style), {}, {
      zIndex: zIndex
    })
  }, rest, {
    afterVisibleChange: handleAfterVisibleChange,
    onClose: onClose,
    open: visible,
    level: null,
    placement: placement,
    showMask: mask,
    handler: false,
    width: width,
    ref: rcRef,
    contentWrapperStyle: contentWrapperStyle
  }), renderContent()));
};

function DrawerHeader(_ref) {
  var _ref$closable = _ref.closable,
      closable = _ref$closable === void 0 ? true : _ref$closable,
      actions = _ref.actions,
      _ref$actionMaxCount = _ref.actionMaxCount,
      actionMaxCount = _ref$actionMaxCount === void 0 ? 4 : _ref$actionMaxCount,
      content = _ref.content,
      style = _ref.style,
      onClose = _ref.onClose,
      actionComponent = _ref.actionComponent,
      onlyShowContent = _ref.onlyShowContent,
      approvalSetting = _ref.approvalSetting,
      detailInfo = _ref.detailInfo;
  var drawer = (0, _react.useContext)(DrawerContext);
  var manager = (0, _react.useContext)(_context["default"]);
  var showClose = closable && (!!onClose || !!drawer);
  if (!showClose && !(actions && actions.length) && !content) return null;
  var displayActions = actions || [];
  var collapsedActions = [];

  if (actions && actions.length > actionMaxCount) {
    displayActions = actions.slice(0, actionMaxCount - 1);
    collapsedActions = actions.slice(actionMaxCount - 1);
  }

  var showBackBtn = !!manager && manager.getCanBack();
  var hasContent = !!content;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "titaui-drawer-header",
    style: style
  }, showBackBtn && /*#__PURE__*/_react["default"].createElement("a", {
    className: "titaui-drawer-header-back",
    onClick: manager && manager.goBack
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "tu-icon-fanhui"
  }), (0, _getLocale.getLocale)('Mod_Back')), showBackBtn && hasContent && /*#__PURE__*/_react["default"].createElement("div", {
    className: "titaui-drawer-header-divider"
  }), hasContent && content, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      flex: 1
    }
  }), /*#__PURE__*/_react["default"].createElement("div", null, onlyShowContent), /*#__PURE__*/_react["default"].createElement("div", null, actionComponent), displayActions.map(function (action) {
    return /*#__PURE__*/_react["default"].createElement(_headerAction["default"], action);
  }), !!collapsedActions.length && /*#__PURE__*/_react["default"].createElement(_headerAction["default"], {
    approvalSetting: approvalSetting,
    detailInfo: detailInfo,
    title: (0, _getLocale.getLocale)('Mod_MoreOperate'),
    icon: "tu-icon-10",
    children: collapsedActions
  }), showClose && /*#__PURE__*/_react["default"].createElement("div", {
    className: "titaui-drawer-header-close tu-icon-canceled",
    onClick: function onClick() {
      drawer && drawer.close();
    }
  }));
}

Drawer.Header = DrawerHeader;
var _default = Drawer;
exports["default"] = _default;