"use strict";

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

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

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

var _dialog = _interopRequireDefault(require("./dialog"));

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

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

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

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 _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 noop = function noop() {};

var StepDialog =
/*#__PURE__*/
function (_React$Component) {
  _inherits(StepDialog, _React$Component);

  function StepDialog(props) {
    var _this;

    _classCallCheck(this, StepDialog);

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

    _defineProperty(_assertThisInitialized(_this), "onStepChange", function (current) {
      if (!('current' in _this.props)) {
        _this.setState({
          current: current
        });
      }

      _this.props.onStepChange(current);
    });

    _defineProperty(_assertThisInitialized(_this), "getViewMeta", function () {
      var children = _this.props.children;
      var meta = [];

      _react["default"].Children.forEach(children, function (c) {
        if (c) {
          meta.push({
            id: c.props.id,
            title: c.props.title,
            footer: c.props.footer
          });
        }
      });

      return meta;
    });

    _defineProperty(_assertThisInitialized(_this), "checkView", function () {
      var children = _this.props.children;
      var isView = true;

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

      _react["default"].Children.forEach(children, function (c) {
        if (c && c.type && c.type.displayName !== 'StepView') {
          isView = false;
        }
      });

      return isView;
    });

    _this.state = {
      current: props.current || props.defaultCurrent
    };
    return _this;
  }

  _createClass(StepDialog, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (nextProps.current !== this.props.current) {
        this.setState({
          current: nextProps.current
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.checkView()) {
        return null;
      }

      var _this$props = this.props,
          children = _this$props.children,
          prefix = _this$props.prefix;
      var metas = this.getViewMeta();
      var current = this.state.current;
      var meta = metas.find(function (m) {
        return m.id === current;
      }) || {};
      var index = metas.findIndex(function (m) {
        return m.id === current;
      });

      var child = _react["default"].Children.toArray(children).find(function (c) {
        return c.props.id === current;
      });

      var title = index === 0 ? _react["default"].createElement("span", null, meta.title) :
      /* eslint no-script-url: 1 */
      _react["default"].createElement("span", null, _react["default"].createElement("a", {
        role: "button",
        href: "javascript:;",
        className: "".concat(prefix, "dialog-prev")
      }, _react["default"].createElement(_button["default"].Icon, {
        name: "chevron-left",
        type: "primary",
        color: "gray",
        onClick: this.onStepChange.bind(this, metas[index - 1].id)
      })), _react["default"].createElement("span", null, meta.title));
      return _react["default"].createElement(_dialog["default"], _extends({}, this.props, {
        title: title,
        footer: meta.footer
      }), child);
    }
  }]);

  return StepDialog;
}(_react["default"].Component);

_defineProperty(StepDialog, "propTypes", {
  prefix: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  className: _propTypes["default"].string,

  /**
   * 是否显示
   */
  visible: _propTypes["default"].bool,

  /**
   * 内容
   */
  children: _propTypes["default"].node,

  /**
   * 应用于取消按钮的属性对象
   */
  cancelProps: _propTypes["default"].object,

  /**
   * 控制对话框关闭的方式，值可以为字符串或者布尔值，其中字符串是由以下值组成：
   * **close** 表示点击关闭按钮可以关闭对话框
   * **mask** 表示点击遮罩区域可以关闭对话框
   * **esc** 表示按下 esc 键可以关闭对话框
   * 如 'close' 或 'close,esc,mask'
   * 如果设置为 true，则以上关闭方式全部生效
   * 如果设置为 false，则以上关闭方式全部失效
   */
  closeable: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].bool]),

  /**
   * 对话框关闭时触发的回调函数
   * @param {String} trigger 关闭触发行为的描述字符串
   * @param {Object} event 关闭时事件对象
   */
  onClose: _propTypes["default"].func,

  /**
   * 对话框关闭后触发的回调函数, 如果有动画，则在动画结束后触发
   */
  afterClose: _propTypes["default"].func,

  /**
   * 是否显示遮罩
   */
  hasMask: _propTypes["default"].bool,

  /**
   * 显示隐藏时动画的播放方式
   * @property {String} in 进场动画
   * @property {String} out 出场动画
   */
  animation: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].bool]),

  /**
   * 对话框弹出时是否自动获得焦点
   */
  autoFocus: _propTypes["default"].bool,

  /**
   * 对话框对齐方式, 具体见Overlay文档
   */
  align: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].bool]),

  /**
   * 当对话框高度超过浏览器视口高度时，是否显示所有内容而不是出现滚动条以保证对话框完整显示在浏览器视口内，该属性仅在对话框垂直水平居中时生效，即 align 被设置为 'cc cc' 时
   */
  isFullScreen: _propTypes["default"].bool,

  /**
   * 是否在对话框重新渲染时及时更新对话框位置，一般用于对话框高度变化后依然能保证原来的对齐方式
   */
  shouldUpdatePosition: _propTypes["default"].bool,

  /**
   * 对话框距离浏览器顶部和底部的最小间距，align 被设置为 'cc cc' 并且 isFullScreen 被设置为 true 时不生效
   */
  minMargin: _propTypes["default"].number,

  /**
   * 透传到弹层组件的属性对象
   */
  overlayProps: _propTypes["default"].object,

  /**
   * 对话框的高度样式属性
   */
  height: _propTypes["default"].string,

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

  /**
   * 当前步骤屏id，受控
   */
  current: _propTypes["default"].string,

  /**
   * 默认步骤屏id，非受控
   */
  defaultCurrent: _propTypes["default"].string,

  /**
   * 步骤屏发生变化时的回调
   */
  onStepChange: _propTypes["default"].func,

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

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

_defineProperty(StepDialog, "defaultProps", {
  prefix: 'next-',
  visible: false,
  closeable: 'esc,close',
  onClose: noop,
  afterClose: noop,
  hasMask: true,
  animation: {
    "in": 'fadeInDown',
    out: 'fadeOutUp'
  },
  autoFocus: false,
  align: 'cc cc',
  isFullScreen: false,
  shouldUpdatePosition: false,
  minMargin: 40,
  overlayProps: {},
  noBodyPadding: false,
  onStepChange: noop,
  loading: false
});

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

exports["default"] = _default;