"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.GroupComponent = void 0;
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _boxSlotComponent = _interopRequireDefault(require("../../_components/boxSlotComponent"));
var _locale = require("../../../../src/locale");
var _type = require("../../cGroupManage-setter/type");
var _schemaService = require("../../../_utils/schemaService");
var _renderByFormKey = _interopRequireDefault(require("../../_components/renderByFormKey"));
var _formKeyType = require("../../../_utils/formKeyType");
var _type2 = require("../../../../src/utils/type");
var _config = require("../config");
var _utils = require("../config/utils");
var _utils2 = require("../../../_utils/utils");
var _type3 = require("../../../../src/utils/CLowCodeService/type");
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 GroupComponent = exports.GroupComponent = function GroupComponent(props) {
  var _useState = (0, _react.useState)(props.value),
    groupData = _useState[0],
    setGroupData = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    groupDataSource = _useState2[0],
    setGroupDataSource = _useState2[1];
  // 组索引数据
  var _useState3 = (0, _react.useState)([]),
    groupIndexDataSource = _useState3[0],
    setGroupIndexDataSource = _useState3[1];
  // 由于schema的组管理存储信息有限需要更具组ID重新获取
  var _useState4 = (0, _react.useState)(),
    groupInfo = _useState4[0],
    setGroupInfo = _useState4[1];
  var _useState5 = (0, _react.useState)([]),
    relationOption = _useState5[0],
    setRelationOption = _useState5[1];
  (0, _react.useEffect)(function () {
    var schemaService = new _schemaService.SchemaService();
    var gList = schemaService.getPageGroupListData();
    setGroupDataSource(gList);
    if (groupData.groupId) {
      initGroupInfo(gList, groupData.groupId);
    }
    setRelationOption((0, _formKeyType.getRelationList)());
  }, []);
  (0, _react.useEffect)(function () {
    if (groupDataSource.length) {
      initGroupInfo(groupDataSource, groupData.groupId);
    }
  }, [groupData.groupId]);
  (0, _react.useEffect)(function () {
    props.onChange(groupData);
  }, [groupData]);
  var getGroupInfo = function getGroupInfo(arr, groupId) {
    var gI = arr.filter(function (item) {
      return item.groupId === groupId;
    });
    if (gI.length) {
      return gI[0];
    }
  };
  var initGroupInfo = function initGroupInfo(arr, groupId) {
    var gInfo = getGroupInfo(arr, groupId);
    if (gInfo) {
      setGroupInfo(gInfo);
      setGroupIndexList(gInfo.groupList);
    }
  };
  var setGroupIndexList = function setGroupIndexList(arr) {
    var result = [];
    arr.forEach(function (item) {
      result.push({
        label: item.groupIndex,
        value: item.groupIndex
      });
    });
    setGroupIndexDataSource(result);
  };
  var isSelectGroupId = function isSelectGroupId(groupId) {
    if (typeof groupId === 'string') {
      return groupId.length > 0;
    } else if (typeof groupId === 'number') {
      return groupId > 0;
    }
    return false;
  };
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupId")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: groupData.groupId,
    dataSource: groupDataSource,
    onChange: function onChange(v) {
      var newGroupData = {
        groupId: v,
        groupIndex: '',
        groupIndexSelectType: _type.GroupDataSourceType["default"]
      };
      // 抹除状态和后续状态
      setGroupData(newGroupData);
    }
  })), props.originNode === _type3.BehaviorType.openBehavior && (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupCategory) === _type.groupCategoryType["default"] && (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupType) === _type.groupClassType.component && (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupMode) === _type.groupModeType.single && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.startAnimation")
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    checked: groupData.startAnimationType,
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        startAnimationType: v
      });
      setGroupData(newGroupData);
    }
  }), groupData.startAnimationType && /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      marginLeft: '10px'
    }
  }, (0, _locale.t)("page.animationTypeTip")))), groupData.startAnimationType && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.animationType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    hasClear: true,
    value: groupData.animationType,
    dataSource: (0, _config.getGroupAnimationOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        animationType: v
      });
      setGroupData(newGroupData);
    }
  }))), props.originNode === _type3.BehaviorType.closeBehavior && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.animationType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    hasClear: true,
    value: groupData.closeAnimationType,
    dataSource: (0, _config.getGroupCloseAnimationOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        closeAnimationType: v
      });
      setGroupData(newGroupData);
    }
  })), !groupData.startAnimationType && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "groupComponentIndexBox"
  }, isSelectGroupId(groupData.groupId) && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndex")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: groupData.groupIndexSelectType,
    dataSource: (0, _config.getGroupTypeOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        groupIndexSelectType: v
      });
      // 抹除状态和后续状态
      setGroupData(newGroupData);
    }
  })), isSelectGroupId(groupData.groupId) && groupData.groupIndexSelectType === _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndex"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: relationOption,
    value: groupData.groupIndexCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, ''),
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var oldData = groupData.groupIndexCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '');
        var newData = (0, _extends2["default"])({}, oldData, newItem);
        setGroupData((0, _extends2["default"])({}, groupData, {
          groupIndexCustom: newData
        }));
      });
    }
  })), isSelectGroupId(groupData.groupId) && groupData.groupIndexSelectType !== _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndex"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: groupData.groupIndex,
    dataSource: groupIndexDataSource,
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        groupIndex: v
      });
      // 抹除状态和后续状态
      setGroupData(newGroupData);
    }
  }))), ((groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupType) === _type.groupClassType.slot || (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupType) === _type.groupClassType.component && (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupMode) === _type.groupModeType.schedule) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "groupComponentIndexBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.stateNameData")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: groupData.groupStateSelectType || _type.GroupDataSourceType["default"],
    dataSource: (0, _config.getGroupTypeOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        groupStateSelectType: v
      });
      setGroupData(newGroupData);
    }
  })), groupData.groupStateSelectType === _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndex"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: relationOption,
    value: groupData.stateCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, ''),
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var oldData = groupData.stateCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '');
        var newData = (0, _extends2["default"])({}, oldData, newItem);
        setGroupData((0, _extends2["default"])({}, groupData, {
          stateCustom: newData
        }));
      });
    }
  })), groupData.groupStateSelectType !== _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(GroupStateComponent, {
    groupInfo: groupInfo,
    groupIndex: groupData.groupIndex,
    value: groupData.state,
    onChange: function onChange(v) {
      setGroupData((0, _extends2["default"])({}, groupData, {
        state: v
      }));
    }
  })), (groupInfo === null || groupInfo === void 0 ? void 0 : groupInfo.groupMode) === _type.groupModeType.schedule && /*#__PURE__*/_react["default"].createElement("div", {
    className: "groupComponentIndexBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.stateNameData")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: groupData.groupFollowStateSelectType || _type.GroupDataSourceType["default"],
    dataSource: (0, _config.getGroupTypeOptionList)(),
    onChange: function onChange(v) {
      var newGroupData = (0, _extends2["default"])({}, groupData, {
        groupFollowStateSelectType: v
      });
      setGroupData(newGroupData);
    }
  })), groupData.groupFollowStateSelectType === _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.groupIndex"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: relationOption,
    value: groupData.followStateCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, ''),
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var oldData = groupData.followStateCustom || (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '');
        var newData = (0, _extends2["default"])({}, oldData, newItem);
        setGroupData((0, _extends2["default"])({}, groupData, {
          followStateCustom: newData
        }));
      });
    }
  })), groupData.groupFollowStateSelectType !== _type.GroupDataSourceType.custom && /*#__PURE__*/_react["default"].createElement(GroupFollowStateComponent, {
    groupInfo: groupInfo,
    groupIndex: groupData.groupIndex,
    value: groupData.followState,
    onChange: function onChange(v) {
      setGroupData((0, _extends2["default"])({}, groupData, {
        followState: v
      }));
    }
  }))));
};
var FollowStateBaseList = [{
  label: (0, _locale.t)('page.stateDefaultName'),
  value: _type2.EngineRuleState.loading
}, {
  label: (0, _locale.t)('page.stateSuccessName'),
  value: _type2.EngineRuleState.finished
}, {
  label: (0, _locale.t)('page.stateFailName'),
  value: _type2.EngineRuleState.failed
}];
var StateBaseList = [{
  label: (0, _locale.t)('button.loadingStyle'),
  value: _type2.EngineRuleState.loading
}, {
  label: (0, _locale.t)('button.selectStyle'),
  value: _type2.EngineRuleState.finished
}, {
  label: (0, _locale.t)('button.errorStyle'),
  value: _type2.EngineRuleState.failed
}];
var GroupStateComponent = function GroupStateComponent(props) {
  var groupInfo = props.groupInfo,
    groupIndex = props.groupIndex,
    value = props.value,
    onChange = props.onChange;
  var _useState6 = (0, _react.useState)([]),
    stateList = _useState6[0],
    setStateList = _useState6[1];
  (0, _react.useEffect)(function () {
    // 组件模式的，进度，后续
    if (groupInfo.groupType === _type.groupClassType.component) {
      setStateList(StateBaseList);
    } else if (groupInfo.groupType === _type.groupClassType.slot) {
      var groupItem = groupInfo.groupList.find(function (item) {
        return item.groupIndex === groupIndex;
      });
      if (groupItem) {
        var arr = groupItem.stateList.map(function (item) {
          if (item.code) {
            return (0, _extends2["default"])({}, item, {
              label: item.name,
              value: item.state === _type2.EngineRuleState.succeeded ? _type2.EngineRuleState.finished : item.state
            });
          }
        }).filter(Boolean);
        setStateList(arr);
      }
    }
  }, [groupInfo, groupIndex]);
  return /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.stateNameData"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    hasClear: true,
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: value,
    dataSource: stateList,
    onChange: onChange
  }));
};
var GroupFollowStateComponent = function GroupFollowStateComponent(props) {
  var groupInfo = props.groupInfo,
    groupIndex = props.groupIndex,
    value = props.value,
    onChange = props.onChange;
  var _useState7 = (0, _react.useState)(FollowStateBaseList),
    followStateList = _useState7[0],
    setFollowStateList = _useState7[1];
  (0, _react.useEffect)(function () {
    var list = groupInfo.groupType === _type.groupClassType.component ? StateBaseList : FollowStateBaseList;
    setFollowStateList(list);
  }, [groupInfo, groupIndex]);
  return /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("page.anomalyAfter"),
    titleStyle: {
      display: 'none'
    }
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    hasClear: true,
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: value,
    dataSource: followStateList,
    onChange: onChange
  }));
};