"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var _extends3 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var React = _react;
require("./index.scss");
var _linkAction = require("./actions/link-action");
var _dialogAction = require("./actions/dialog-action");
var _messageAction = require("./actions/message-action");
var _tooltipAction = require("./actions/tooltip-action");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var defaultActionMap = {
  dialog: _dialogAction.dialogBehaviorAction,
  link: _linkAction.linkBehaviorAction,
  tooltip: _tooltipAction.tooltipBehaviorAction,
  message: _messageAction.messageBehaviorAction
};
var BehaviorSetter = function BehaviorSetter(_ref) {
  var _ref$value = _ref.value,
    behaviors = _ref$value === void 0 ? {} : _ref$value,
    onChange = _ref.onChange,
    propsField = _ref.field,
    prop = _ref.prop,
    actions = _ref.actions,
    propsType = _ref.type,
    _ref$extraBehaviorAct = _ref.extraBehaviorActions,
    propsBehaviorActions = _ref$extraBehaviorAct === void 0 ? [] : _ref$extraBehaviorAct,
    _ref$extendedOptions = _ref.extendedOptions,
    extendedOptions = _ref$extendedOptions === void 0 ? {} : _ref$extendedOptions,
    url = _ref.url,
    responseFormatter = _ref.responseFormatter,
    enableMessageAction = _ref.enableMessageAction,
    enableTooltipAction = _ref.enableTooltipAction;
  var field = propsField || prop;
  var _useState = (0, _react.useState)(actions[0]),
    currentEvent = _useState[0],
    setCurrentEvent = _useState[1];
  /** 当前的行为集合 */

  var currentBehavior = behaviors[currentEvent] || {};
  var currentBehaviorType = currentBehavior.type || propsType || 'dialog';
  var defaultActions = [];
  defaultActions.push(defaultActionMap.dialog, defaultActionMap.link);
  if (enableMessageAction) {
    defaultActions.push(defaultActionMap.message);
  }
  if (enableTooltipAction) {
    defaultActions.push(defaultActionMap.tooltip);
  }
  var behaviorActions = (0, _react.useMemo)(function () {
    return [].concat(defaultActions, propsBehaviorActions);
  }, [propsBehaviorActions]);
  var updateCurrentEventBehavior = function updateCurrentEventBehavior(type, newVal) {
    var _currentEvent, _extends2;
    onChange((0, _extends3["default"])({}, behaviors, (_extends2 = {}, _extends2[currentEvent] = (_currentEvent = {
      type: type
    }, _currentEvent[type] = newVal, _currentEvent), _extends2)));
  };
  var behaviorOption = behaviorActions.find(function (vo) {
    return vo.name === currentBehaviorType;
  });
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u4E8B\u4EF6\u5217\u8868"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, /*#__PURE__*/React.createElement(_select["default"], {
    size: "small",
    dataSource: actions.map(function (action) {
      return {
        label: action,
        value: action
      };
    }),
    value: currentEvent,
    onChange: function onChange(eventName) {
      return setCurrentEvent(eventName);
    }
  }))), /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u64CD\u4F5C\u7C7B\u578B"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, /*#__PURE__*/React.createElement(_radio["default"].Group, {
    size: "small",
    dataSource: behaviorActions.map(function (vo) {
      return {
        value: vo.name,
        label: vo.title,
        disabled: propsType && vo.name !== propsType
      };
    }),
    value: currentBehaviorType,
    shape: "button",
    onChange: function onChange(behaviorType) {
      return updateCurrentEventBehavior(behaviorType, undefined);
    }
  }))), /*#__PURE__*/React.createElement(ErrorBoundary, null, behaviorOption && behaviorOption.render({
    field: field,
    value: currentBehavior[currentBehaviorType],
    onChange: function onChange(behaviorValue) {
      field.parent.setPropValue(currentEvent, behaviorOption.toActionValue(behaviorValue, extendedOptions[currentBehaviorType]));
      updateCurrentEventBehavior(currentBehaviorType, behaviorValue);
    },
    options: currentBehaviorType === 'link' ? (0, _extends3["default"])({}, extendedOptions[currentBehaviorType], {
      url: url,
      responseFormatter: responseFormatter
    }) : extendedOptions[currentBehaviorType]
  })));
};
var _default = exports["default"] = BehaviorSetter; // eslint-disable-next-line @iceworks/best-practices/recommend-functional-component
var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
  function ErrorBoundary() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      hasError: false,
      error: undefined
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(ErrorBoundary, _React$Component);
  var _proto = ErrorBoundary.prototype;
  _proto.componentDidCatch = function componentDidCatch(error, errorInfo) {
    // 你同样可以将错误日志上报给服务器
    console.error(error, errorInfo);
  };
  _proto.render = function render() {
    if (this.state.hasError) {
      var _this$state$error;
      // 你可以自定义降级后的 UI 并渲染
      return /*#__PURE__*/React.createElement("div", null, "\u6E32\u67D3\u5F02\u5E38: ", ((_this$state$error = this.state.error) === null || _this$state$error === void 0 ? void 0 : _this$state$error.message) || this.state.error || '');
    }
    return this.props.children;
  };
  return ErrorBoundary;
}(React.Component);
// 更新 state 使下一次渲染能够显示降级后的 UI
ErrorBoundary.getDerivedStateFromError = function (error) {
  return {
    hasError: true,
    error: error
  };
};