"use strict";

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

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

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

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

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

var _loading = _interopRequireDefault(require("../loading"));

var _zhCn = _interopRequireDefault(require("../locale/zh-cn"));

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 _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 _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); 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 makeChain = _util.func.makeChain;
var pickAttrs = _util.obj.pickAttrs;

var noop = function noop() {};

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

  function Inner() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Inner);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Inner)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "getNode", function (name, ref) {
      _this[name] = ref;
    });

    _defineProperty(_assertThisInitialized(_this), "renderHeader", function () {
      var _this$props = _this.props,
          prefix = _this$props.prefix,
          title = _this$props.title;

      var closeLink = _this.renderCloseLink();

      if (title) {
        _this.titleId = (0, _util.guid)('dialog-title-');
        return _react["default"].createElement("div", {
          className: "".concat(prefix, "dialog-header"),
          id: _this.titleId,
          ref: _this.getNode.bind(_assertThisInitialized(_this), 'headerNode'),
          role: "heading",
          "aria-level": "1"
        }, title, closeLink);
      }

      return null;
    });

    _defineProperty(_assertThisInitialized(_this), "renderBody", function () {
      var _this$props2 = _this.props,
          prefix = _this$props2.prefix,
          children = _this$props2.children,
          noBodyPadding = _this$props2.noBodyPadding;

      if (children) {
        var _cx;

        return _react["default"].createElement("div", {
          className: (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(prefix, "dialog-body"), true), _defineProperty(_cx, "".concat(prefix, "dialog-body-no-padding"), noBodyPadding), _cx)),
          ref: _this.getNode.bind(_assertThisInitialized(_this), 'bodyNode')
        }, children);
      }

      return null;
    });

    _defineProperty(_assertThisInitialized(_this), "renderFooter", function () {
      var _cx2;

      var _this$props3 = _this.props,
          prefix = _this$props3.prefix,
          footer = _this$props3.footer,
          footerAlign = _this$props3.footerAlign,
          footerActions = _this$props3.footerActions,
          locale = _this$props3.locale,
          height = _this$props3.height;

      if (footer === false) {
        return null;
      }

      var newClassName = (0, _classnames["default"])((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "dialog-footer"), true), _defineProperty(_cx2, "".concat(prefix, "align-").concat(footerAlign), true), _defineProperty(_cx2, "".concat(prefix, "dialog-footer-fixed-height"), !!height), _cx2));
      var footerContent = footer === true || !footer ? footerActions.map(function (action) {
        var btnProps = _this.props["".concat(action, "Props")];

        var newBtnProps = _objectSpread({}, btnProps, {
          prefix: prefix,
          className: (0, _classnames["default"])("".concat(prefix, "dialog-btn"), btnProps.className),
          onClick: makeChain(_this.props["on".concat(action[0].toUpperCase() + action.slice(1))], btnProps.onClick),
          children: btnProps.children || locale[action]
        });

        if (action === 'ok') {
          newBtnProps.type = 'primary';
        }

        return _react["default"].createElement(_button["default"], _extends({
          key: action
        }, newBtnProps));
      }) : footer;
      return _react["default"].createElement("div", {
        className: newClassName,
        ref: _this.getNode.bind(_assertThisInitialized(_this), 'footerNode')
      }, footerContent);
    });

    _defineProperty(_assertThisInitialized(_this), "renderCloseLink", function () {
      var _this$props4 = _this.props,
          prefix = _this$props4.prefix,
          closeable = _this$props4.closeable,
          onClose = _this$props4.onClose,
          locale = _this$props4.locale;

      if (closeable) {
        return _react["default"].createElement("a", {
          role: "button",
          "aria-label": locale.close,
          href: "javascript:;",
          className: "".concat(prefix, "dialog-close"),
          onClick: onClose
        }, _react["default"].createElement(_button["default"].Icon, {
          type: "primary",
          color: "gray",
          className: "".concat(prefix, "dialog-close-icon"),
          name: "remove"
        }));
      }

      return null;
    });

    return _this;
  }

  _createClass(Inner, [{
    key: "render",
    value: function render() {
      var _cx3;

      var _this$props5 = this.props,
          prefix = _this$props5.prefix,
          className = _this$props5.className,
          size = _this$props5.size,
          closeable = _this$props5.closeable,
          title = _this$props5.title,
          role = _this$props5.role,
          rtl = _this$props5.rtl,
          height = _this$props5.height,
          loading = _this$props5.loading,
          loadingTip = _this$props5.loadingTip,
          others = _objectWithoutProperties(_this$props5, ["prefix", "className", "size", "closeable", "title", "role", "rtl", "height", "loading", "loadingTip"]);

      var newClassName = (0, _classnames["default"])((_cx3 = {}, _defineProperty(_cx3, "".concat(prefix, "dialog"), true), _defineProperty(_cx3, "".concat(prefix, "dialog-").concat(size), true), _defineProperty(_cx3, "".concat(prefix, "closeable"), closeable), _defineProperty(_cx3, className, !!className), _cx3));
      var header = this.renderHeader();
      var body = this.renderBody();
      var footer = this.renderFooter();
      var ariaProps = {
        role: role,
        'aria-modal': 'true'
      };

      if (title) {
        ariaProps['aria-labelledby'] = this.titleId;
      }

      others.style = _extends({}, others.style, {
        height: height
      });
      return _react["default"].createElement("div", _extends({}, ariaProps, {
        className: newClassName
      }, pickAttrs(others), {
        dir: rtl ? 'rtl' : undefined
      }), _react["default"].createElement(_loading["default"], {
        visible: loading,
        tip: loadingTip,
        inline: false
      }, header, body, footer));
    }
  }]);

  return Inner;
}(_react.Component);

exports["default"] = Inner;

_defineProperty(Inner, "propTypes", {
  prefix: _propTypes["default"].string,
  className: _propTypes["default"].string,
  size: _propTypes["default"].oneOf(['small', 'medium']),
  title: _propTypes["default"].node,
  children: _propTypes["default"].node,
  footer: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].node]),
  footerAlign: _propTypes["default"].oneOf(['left', 'center', 'right']),
  footerActions: _propTypes["default"].array,
  onOk: _propTypes["default"].func,
  onCancel: _propTypes["default"].func,
  okProps: _propTypes["default"].object,
  cancelProps: _propTypes["default"].object,
  closeable: _propTypes["default"].bool,
  onClose: _propTypes["default"].func,
  locale: _propTypes["default"].object,
  role: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  // set value for a fixed height dialog. Passing a value will absolutely position the footer to the bottom.
  height: _propTypes["default"].string,

  /**
   * 没有内容区的padding
   */
  noBodyPadding: _propTypes["default"].bool,

  /**
   * 是否处于加载状态
   */
  loading: _propTypes["default"].bool,

  /**
   * 加载提示
   */
  loadingTip: _propTypes["default"].node
});

_defineProperty(Inner, "defaultProps", {
  prefix: 'next-',
  size: 'medium',
  footerAlign: 'right',
  footerActions: ['ok', 'cancel'],
  onOk: noop,
  onCancel: noop,
  okProps: {},
  cancelProps: {},
  closeable: true,
  onClose: noop,
  locale: _zhCn["default"].Dialog,
  role: 'dialog',
  noBodyPadding: false,
  loading: false
});