"use strict";

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

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

require("antd/es/drawer/style");

var _drawer = _interopRequireDefault(require("antd/es/drawer"));

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

var _lodashEs = require("lodash-es");

var _ahooks = require("ahooks");

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

var _configProvider = _interopRequireDefault(require("../config-provider"));

var _excluded = ["uniqueKey", "prefixCls", "afterClose", "content", "children"];

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 InlineDrawer = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var uniqueKey = props.uniqueKey,
      customizePrefixCls = props.prefixCls,
      afterClose = props.afterClose,
      content = props.content,
      children = props.children,
      restProps = _objectWithoutProperties(props, _excluded);

  var _useContext = (0, _react.useContext)(_configProvider.default.Context),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('drawer', customizePrefixCls);
  var Component = children || content;

  var _useSetState = (0, _ahooks.useSetState)({
    _visible: props.visible
  }),
      _useSetState2 = _slicedToArray(_useSetState, 2),
      state = _useSetState2[0],
      setState = _useSetState2[1];

  var batchUpdateDrawersStyle = function batchUpdateDrawersStyle(operateClassName, operateType) {
    var nodeList = Array.from(document.getElementsByClassName(operateClassName));
    var len = nodeList.length;
    nodeList.forEach(function (node, index) {
      var steps = len - index;
      if (operateType === 'close') steps = len - index - 1;
      node.style.transform = "translateX(-".concat(180 * steps, "px)");
    });
  };

  (0, _react.useEffect)(function () {
    if (state._visible) {
      // 展开,将前面同一层级下的所有抽屉往左移动180px
      batchUpdateDrawersStyle('ant-drawer-open', 'open');
    } else {
      // 收起，移回上次位置
      batchUpdateDrawersStyle('ant-drawer-open', 'close');
    }
  }, [state._visible]);

  var _onClose = function onClose() {
    var _props$onClose;

    setState({
      _visible: false
    });
    (_props$onClose = props.onClose) === null || _props$onClose === void 0 ? void 0 : _props$onClose.call(props);
  };

  var afterVisibleChange = function afterVisibleChange(visible) {
    var _props$afterVisibleCh;

    if (!visible) afterClose === null || afterClose === void 0 ? void 0 : afterClose();
    (_props$afterVisibleCh = props.afterVisibleChange) === null || _props$afterVisibleCh === void 0 ? void 0 : _props$afterVisibleCh.call(props, visible);
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      close: _onClose
    };
  });
  return /*#__PURE__*/_react.default.createElement(_drawer.default, Object.assign({}, restProps, {
    onClose: _onClose,
    visible: state._visible,
    afterVisibleChange: afterVisibleChange,
    className: (0, _classnames.default)(prefixCls, props.className)
  }), _react.default.Children.map(Component, function (child) {
    if ((0, _lodashEs.isFunction)(child.type)) {
      return /*#__PURE__*/_react.default.cloneElement(child, {
        onClose: function onClose() {
          var _child$props, _child$props$onClose;

          _onClose();

          for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
            arg[_key] = arguments[_key];
          }

          child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : (_child$props$onClose = _child$props.onClose) === null || _child$props$onClose === void 0 ? void 0 : _child$props$onClose.call.apply(_child$props$onClose, [_child$props].concat(arg));
        }
      });
    }

    return child;
  }));
});
InlineDrawer.defaultProps = {
  width: 434,
  visible: false,
  getContainer: false
};
var _default = InlineDrawer;
exports.default = _default;