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

var _vue = _interopRequireWildcard(require("vue"));

var _classNames = _interopRequireDefault(require("../_util/classNames"));

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

var _src = _interopRequireDefault(require("../vc-drawer/src"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons-vue/CloseOutlined"));

var _propsUtil = require("../_util/props-util");

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

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 _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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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; }

var Drawer = {
  name: 'ADrawer',
  inheritAttrs: false,
  props: {
    closable: _vueTypes.default.bool.def(true),
    destroyOnClose: _vueTypes.default.bool,
    getContainer: _vueTypes.default.any,
    maskClosable: _vueTypes.default.bool.def(true),
    mask: _vueTypes.default.bool.def(true),
    maskStyle: _vueTypes.default.object,
    wrapStyle: _vueTypes.default.object,
    bodyStyle: _vueTypes.default.object,
    headerStyle: _vueTypes.default.object,
    drawerStyle: _vueTypes.default.object,
    title: _vueTypes.default.any,
    visible: _vueTypes.default.bool,
    width: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number]).def(256),
    height: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number]).def(256),
    zIndex: _vueTypes.default.number,
    prefixCls: _vueTypes.default.string,
    placement: _vueTypes.default.oneOf(['top', 'right', 'bottom', 'left']).def('right'),
    level: _vueTypes.default.any.def(null),
    wrapClassName: _vueTypes.default.string,
    // not use class like react, vue will add class to root dom
    handle: _vueTypes.default.any,
    afterVisibleChange: _vueTypes.default.func,
    keyboard: _vueTypes.default.bool.def(true),
    onClose: _vueTypes.default.func,
    'onUpdate:visible': _vueTypes.default.func
  },
  mixins: [_BaseMixin.default],
  data: function data() {
    this.destroyClose = false;
    this.preVisible = this.$props.visible;
    return {
      _push: false
    };
  },
  setup: function setup() {
    var configProvider = (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps);
    return {
      configProvider: configProvider
    };
  },
  beforeCreate: function beforeCreate() {
    var parentDrawer = (0, _vue.inject)('parentDrawer', null);
    (0, _vue.provide)('parentDrawer', this);
    this.parentDrawer = parentDrawer;
  },
  mounted: function mounted() {
    // fix: delete drawer in child and re-render, no push started.
    // <Drawer>{show && <Drawer />}</Drawer>
    var visible = this.visible;

    if (visible && this.parentDrawer) {
      this.parentDrawer.push();
    }
  },
  updated: function updated() {
    var _this = this;

    (0, _vue.nextTick)(function () {
      if (_this.preVisible !== _this.visible && _this.parentDrawer) {
        if (_this.visible) {
          _this.parentDrawer.push();
        } else {
          _this.parentDrawer.pull();
        }
      }

      _this.preVisible = _this.visible;
    });
  },
  beforeUnmount: function beforeUnmount() {
    // unmount drawer in child, clear push.
    if (this.parentDrawer) {
      this.parentDrawer.pull();
    }
  },
  methods: {
    close: function close(e) {
      this.$emit('update:visible', false);
      this.$emit('close', e);
    },
    // onMaskClick(e) {
    //   if (!this.maskClosable) {
    //     return;
    //   }
    //   this.close(e);
    // },
    push: function push() {
      this.setState({
        _push: true
      });
    },
    pull: function pull() {
      this.setState({
        _push: false
      });
    },
    onDestroyTransitionEnd: function onDestroyTransitionEnd() {
      var isDestroyOnClose = this.getDestroyOnClose();

      if (!isDestroyOnClose) {
        return;
      }

      if (!this.visible) {
        this.destroyClose = true;
        this.$forceUpdate();
      }
    },
    getDestroyOnClose: function getDestroyOnClose() {
      return this.destroyOnClose && !this.visible;
    },
    // get drawar push width or height
    getPushTransform: function getPushTransform(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)");
      }
    },
    getRcDrawerStyle: function getRcDrawerStyle() {
      var _this$$props = this.$props,
          zIndex = _this$$props.zIndex,
          placement = _this$$props.placement,
          wrapStyle = _this$$props.wrapStyle;
      var push = this.$data._push;
      return _objectSpread({
        zIndex: zIndex,
        transform: push ? this.getPushTransform(placement) : undefined
      }, wrapStyle);
    },
    renderHeader: function renderHeader(prefixCls) {
      var _this$$props2 = this.$props,
          closable = _this$$props2.closable,
          headerStyle = _this$$props2.headerStyle;
      var title = (0, _propsUtil.getComponent)(this, 'title');

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

      var headerClassName = title ? "".concat(prefixCls, "-header") : "".concat(prefixCls, "-header-no-title");
      return _vue.createVNode("div", {
        "class": headerClassName,
        "style": headerStyle
      }, [title && _vue.createVNode("div", {
        "class": "".concat(prefixCls, "-title")
      }, [title]), closable ? this.renderCloseIcon(prefixCls) : null]);
    },
    renderCloseIcon: function renderCloseIcon(prefixCls) {
      var closable = this.closable;
      return closable && _vue.createVNode("button", {
        "key": "closer",
        "onClick": this.close,
        "aria-label": "Close",
        "class": "".concat(prefixCls, "-close")
      }, [_vue.createVNode(_CloseOutlined.default, null, null)]);
    },
    // render drawer body dom
    renderBody: function renderBody(prefixCls) {
      if (this.destroyClose && !this.visible) {
        return null;
      }

      this.destroyClose = false;
      var _this$$props3 = this.$props,
          bodyStyle = _this$$props3.bodyStyle,
          drawerStyle = _this$$props3.drawerStyle;
      var containerStyle = {};
      var isDestroyOnClose = this.getDestroyOnClose();

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

      return _vue.createVNode("div", {
        "class": "".concat(prefixCls, "-wrapper-body"),
        "style": _objectSpread(_objectSpread({}, containerStyle), drawerStyle),
        "onTransitionend": this.onDestroyTransitionEnd
      }, [this.renderHeader(prefixCls), _vue.createVNode("div", {
        "key": "body",
        "class": "".concat(prefixCls, "-body"),
        "style": bodyStyle
      }, [this.$slots.default && this.$slots.default()])]);
    }
  },
  render: function render() {
    var _classnames,
        _this2 = this;

    var props = (0, _propsUtil.getOptionProps)(this);

    var customizePrefixCls = props.prefixCls,
        width = props.width,
        height = props.height,
        visible = props.visible,
        placement = props.placement,
        wrapClassName = props.wrapClassName,
        mask = props.mask,
        rest = _objectWithoutProperties(props, ["prefixCls", "width", "height", "visible", "placement", "wrapClassName", "mask"]);

    var haveMask = mask ? '' : 'no-mask';
    var offsetStyle = {};

    if (placement === 'left' || placement === 'right') {
      offsetStyle.width = typeof width === 'number' ? "".concat(width, "px") : width;
    } else {
      offsetStyle.height = typeof height === 'number' ? "".concat(height, "px") : height;
    }

    var handler = (0, _propsUtil.getComponent)(this, 'handle') || false;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('drawer', customizePrefixCls);
    var className = this.$attrs.class;

    var vcDrawerProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, this.$attrs), (0, _omit.default)(rest, ['closable', 'destroyOnClose', 'drawerStyle', 'headerStyle', 'bodyStyle', 'title', 'push', 'visible', 'getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'pageHeader', 'autoInsertSpaceInButton'])), {}, {
      onClose: this.close,
      handler: handler
    }, offsetStyle), {}, {
      prefixCls: prefixCls,
      open: visible,
      showMask: mask,
      placement: placement,
      class: (0, _classNames.default)((_classnames = {}, _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, wrapClassName, !!wrapClassName), _defineProperty(_classnames, haveMask, !!haveMask), _classnames)),
      wrapStyle: this.getRcDrawerStyle()
    });

    return _vue.createVNode(_src.default, vcDrawerProps, {
      default: function _default() {
        return [_this2.renderBody(prefixCls)];
      }
    });
  }
};
/* istanbul ignore next */

Drawer.install = function (app) {
  app.component(Drawer.name, Drawer);
};

var _default2 = Drawer;
exports.default = _default2;