"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _button = _interopRequireDefault(require("../button"));

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

var _omit = _interopRequireDefault(require("omit.js"));

var _util = require("../util");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _typeof(obj) { 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); }

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

var DrawerContext = _react["default"].createContext(null);

var Drawer =
/*#__PURE__*/
function (_Component) {
  _inherits(Drawer, _Component);

  function Drawer(props) {
    var _this;

    _classCallCheck(this, Drawer);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Drawer).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "push", function () {
      _this.setState({
        push: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "pull", function () {
      _this.setState({
        push: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onDestroyTransitionEnd", function () {
      var isDestroyOnClose = _this.getDestroyOnClose();

      if (!isDestroyOnClose) {
        return;
      }

      if (!_this.props.visible) {
        _this.destroyClose = true;

        _this.forceUpdate();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getDestroyOnClose", function () {
      return _this.props.destroyOnClose && !_this.props.visible;
    });

    _defineProperty(_assertThisInitialized(_this), "getPushTransform", function (placement) {
      if (placement === 'left' || placement === 'right') {
        return "translateX(".concat(placement === 'left' ? 180 : -180, "px)");
      }

      if (placement === 'top' || placement === 'bottom') {
        return "translateY(".concat(placement === 'top' ? 180 : -180, "px)");
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getRcDrawerStyle", function () {
      var _this$props = _this.props,
          zIndex = _this$props.zIndex,
          placement = _this$props.placement,
          style = _this$props.style;
      var push = _this.state.push;
      return _objectSpread({
        zIndex: zIndex,
        transform: push ? _this.getPushTransform(placement) : undefined
      }, style);
    });

    _defineProperty(_assertThisInitialized(_this), "renderBody", function () {
      var _this$props2 = _this.props,
          bodyStyle = _this$props2.bodyStyle,
          drawerStyle = _this$props2.drawerStyle,
          prefix = _this$props2.prefix,
          visible = _this$props2.visible;

      if (_this.destroyClose && !visible) {
        return null;
      }

      _this.destroyClose = false;
      var containerStyle = {};

      var isDestroyOnClose = _this.getDestroyOnClose();

      if (isDestroyOnClose) {
        // Increase the opacity transition, delete children after closing.
        containerStyle.opacity = 0;
        containerStyle.transition = 'opacity .3s';
      }

      return _react["default"].createElement("div", {
        className: "".concat(prefix, "drawer-wrapper-body"),
        style: _objectSpread({}, containerStyle, {}, drawerStyle),
        onTransitionEnd: _this.onDestroyTransitionEnd
      }, _this.renderHeader(), _react["default"].createElement("div", {
        className: "".concat(prefix, "drawer-body"),
        style: bodyStyle
      }, _this.props.children), _this.renderFooter());
    });

    _defineProperty(_assertThisInitialized(_this), "renderProvider", function (value) {
      var _this$props3 = _this.props,
          prefix = _this$props3.prefix,
          placement = _this$props3.placement,
          className = _this$props3.className,
          width = _this$props3.width,
          height = _this$props3.height,
          mask = _this$props3.mask,
          rest = _objectWithoutProperties(_this$props3, ["prefix", "placement", "className", "width", "height", "mask"]);

      var haveMask = mask ? '' : 'no-mask';
      _this.parentDrawer = value;
      var offsetStyle = {};

      if (placement === 'left' || placement === 'right') {
        offsetStyle.width = width;
      } else {
        offsetStyle.height = height;
      }

      var drawerClassName = (0, _classnames["default"])(className, haveMask);
      return _react["default"].createElement(DrawerContext.Provider, {
        value: _assertThisInitialized(_this)
      }, _react["default"].createElement(_rcDrawer["default"], _extends({
        handler: false
      }, (0, _omit["default"])(rest, ['zIndex', 'style', 'closable', 'destroyOnClose', 'drawerStyle', 'headerStyle', 'bodyStyle', 'footerStyle', 'footer', 'locale', 'title', 'push', 'visible', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty']), offsetStyle, {
        prefixCls: "".concat(prefix, "drawer"),
        open: _this.props.visible,
        placement: placement,
        showMask: mask,
        style: _this.getRcDrawerStyle(),
        className: drawerClassName
      }), _this.renderBody()));
    });

    _this.parentDrawer = _assertThisInitialized(_this) || null;
    _this.destroyClose = false;
    _this.state = {
      push: false
    };
    return _this;
  }

  _createClass(Drawer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var visible = this.props.visible;

      if (visible && this.parentDrawer) {
        this.parentDrawer.push();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(preProps) {
      var visible = this.props.visible;

      if (preProps.visible !== visible && this.parentDrawer) {
        if (visible) {
          this.parentDrawer.push();
        } else {
          this.parentDrawer.pull();
        }
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      // unmount drawer in child, clear push.
      if (this.parentDrawer) {
        this.parentDrawer.pull();
        this.parentDrawer = null;
      }
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      var _this$props4 = this.props,
          title = _this$props4.title,
          prefix = _this$props4.prefix,
          closable = _this$props4.closable,
          headerStyle = _this$props4.headerStyle;

      if (!title && !closable) {
        return null;
      }

      var headerClassName = title ? "".concat(prefix, "drawer-header") : "".concat(prefix, "drawer-header-no-title");
      return _react["default"].createElement("div", {
        className: headerClassName,
        style: headerStyle
      }, title && _react["default"].createElement("div", {
        className: "".concat(prefix, "drawer-title")
      }, title), closable && this.renderCloseIcon());
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      var _this$props5 = this.props,
          footer = _this$props5.footer,
          footerStyle = _this$props5.footerStyle,
          prefix = _this$props5.prefix;

      if (!footer) {
        return null;
      }

      var footerClassName = "".concat(prefix, "drawer-footer");
      return _react["default"].createElement("div", {
        className: footerClassName,
        style: footerStyle
      }, footer);
    }
  }, {
    key: "renderCloseIcon",
    value: function renderCloseIcon() {
      var _this$props6 = this.props,
          closable = _this$props6.closable,
          prefix = _this$props6.prefix,
          onClose = _this$props6.onClose;
      return closable && _react["default"].createElement(_button["default"].Icon, {
        name: "remove",
        type: "secondary",
        color: "gray",
        className: "".concat(prefix, "drawer-close"),
        onClick: onClose,
        style: {
          '--scroll-bar': "".concat(_util.dom.scrollbar().width, "px")
        }
      });
    } // render drawer body dom

  }, {
    key: "render",
    value: function render() {
      return _react["default"].createElement(DrawerContext.Consumer, null, this.renderProvider);
    }
  }]);

  return Drawer;
}(_react.Component);

_defineProperty(Drawer, "propTypes", {
  prefix: _propTypes["default"].string,
  closable: _propTypes["default"].bool,
  destroyOnClose: _propTypes["default"].bool,
  getContainer: _propTypes["default"].any,
  maskClosable: _propTypes["default"].bool,
  mask: _propTypes["default"].bool,
  maskStyle: _propTypes["default"].object,
  style: _propTypes["default"].object,
  drawerStyle: _propTypes["default"].object,
  headerStyle: _propTypes["default"].object,
  bodyStyle: _propTypes["default"].object,
  title: _propTypes["default"].node,
  visible: _propTypes["default"].bool,
  width: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
  height: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
  zIndex: _propTypes["default"].number,
  prefixCls: _propTypes["default"].string,
  push: _propTypes["default"].bool,
  placement: _propTypes["default"].oneOf(['top', 'right', 'bottom', 'left']),
  onClose: _propTypes["default"].func,
  afterVisibleChange: _propTypes["default"].func,
  className: _propTypes["default"].string,
  handler: _propTypes["default"].bool,
  footer: _propTypes["default"].node,
  footerStyle: _propTypes["default"].object,
  keyboard: _propTypes["default"].bool,
  level: _propTypes["default"].any
});

_defineProperty(Drawer, "defaultProps", {
  prefix: 'next-',
  width: 256,
  height: 256,
  closable: true,
  placement: 'right',
  maskClosable: true,
  level: null,
  keyboard: true,
  mask: true
});

var _default = _configProvider["default"].config(Drawer);

exports["default"] = _default;