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