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 { AwaitBehaViorOption } from "../type";
import BoxSlotComponent from "../../_components/boxSlotComponent";
import CLoadingConfigSetter from "../../_components/cLoadingConfig-setter";
import { LoadingConfigOpenMode } from "../../cDataSource-setter/type";
import RenderByFormKey from "../../_components/renderByFormKey";
import { t } from "../../../../src/locale";
import { DataSourceType, getCustomStateTypeRelationList, getRelationList } from "../../../_utils/formKeyType";
import { handleRenderByFormKeyData } from "../../../_utils/utils";
import { getInitStateKey } from "../config/utils";
var AwaitOptions = [{
  label: t("setter.time"),
  value: AwaitBehaViorOption.Time
}, {
  label: t("setter.block"),
  value: AwaitBehaViorOption.Block
}];
export var AwaitComponent = function AwaitComponent(props) {
  var value = props.value,
    onChange = props.onChange;
  var _useState = useState((value === null || value === void 0 ? void 0 : value.type) || AwaitBehaViorOption.Time),
    type = _useState[0],
    setType = _useState[1];
  var _useState2 = useState((value === null || value === void 0 ? void 0 : value.data) || getInitStateKey(DataSourceType.defaultType, '')),
    data = _useState2[0],
    setData = _useState2[1];
  var _useState3 = useState((value === null || value === void 0 ? void 0 : value.loadingConfig) || {
      openType: false,
      openMode: LoadingConfigOpenMode["default"]
    }),
    loadingConfig = _useState3[0],
    setLoadingConfig = _useState3[1];
  var _useState4 = useState((value === null || value === void 0 ? void 0 : value.outputConfig) || {
      openType: false,
      stateKey: getInitStateKey(DataSourceType.customStateType, '')
    }),
    outputConfig = _useState4[0],
    setOutputConfig = _useState4[1];
  var _useState5 = useState([]),
    awaitRelationOption = _useState5[0],
    setAwaitRelationOption = _useState5[1];
  var _useState6 = useState([]),
    relationOption = _useState6[0],
    setRelationOption = _useState6[1];
  useEffect(function () {
    setAwaitRelationOption(getRelationList());
    setRelationOption(getCustomStateTypeRelationList());
  }, []);
  useEffect(function () {
    var obj = {
      type: type,
      data: data,
      loadingConfig: loadingConfig,
      outputConfig: outputConfig
    };
    onChange(obj);
  }, [type, data, loadingConfig, outputConfig]);
  var handleOldData = function handleOldData(data) {
    if (typeof data === 'number' || typeof data === 'string') {
      return getInitStateKey(DataSourceType.defaultType, data);
    }
    return data;
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "awaitComponentBox"
  }, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.awaitType")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: AwaitOptions,
    value: type,
    onChange: function onChange(v) {
      setType(v);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: type === AwaitBehaViorOption.Time ? t("setter.awaitTime") + "(s)" : t("setter.awaitBlock") + "(block)"
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: awaitRelationOption,
    value: handleOldData(data),
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, data, newItem);
        setData(newData);
      });
    }
  }))), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: "loading",
    labelAlign: "top"
  }, /*#__PURE__*/React.createElement(CLoadingConfigSetter, {
    value: loadingConfig,
    onChange: function onChange(e) {
      setLoadingConfig(e);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.awaitCustomStateOpen")
  }, /*#__PURE__*/React.createElement(_Switch, {
    checked: outputConfig.openType,
    onChange: function onChange(v) {
      setOutputConfig(_extends({}, outputConfig, {
        openType: v
      }));
    }
  })), outputConfig.openType && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.awaitCustomState")
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: relationOption,
    value: outputConfig.stateKey,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newCopy = _extends({}, outputConfig.stateKey, newItem);
        setOutputConfig(_extends({}, outputConfig, {
          stateKey: newCopy
        }));
      });
    }
  })));
};