import _CascaderSelect from "@alifd/next/es/cascader-select";
import React, { useEffect, useState } from "react";
import BoxSlotComponent from "../../_components/boxSlotComponent";
import { BehaviorService } from "../../../_utils/behaviorService";
import { OperateEnumType } from "../config";
import { filter_source } from "../../../_utils/utils";
import { CTreeDataRelationSetter } from "../..";
import { getApiParams } from "../../cDataSourceList-setter/commonFn";
import { getDialogParamsByTreeData, getNewKeyByApiParams, getTreeDataNum } from "../config/utils";
import { t } from "../../../../src/locale";
export var DialogComponent = function DialogComponent(props) {
  var _props$value, _props$value2, _props$value3;
  var behaviorService = new BehaviorService();
  var _useState = useState([]),
    dataSource = _useState[0],
    setDataSource = _useState[1];
  var _useState2 = useState(((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.modelName) || []),
    modelName = _useState2[0],
    setModelName = _useState2[1];
  var _useState3 = useState(((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.modelTreeData) || []),
    modelTreeData = _useState3[0],
    setModelTreeData = _useState3[1];
  var _useState4 = useState(((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : _props$value3.apiParams) || []),
    apiParams = _useState4[0],
    setApiParams = _useState4[1];
  useEffect(function () {
    var data = behaviorService.getModalList();
    setDataSource(data);
  }, []);
  useEffect(function () {
    var obj = {
      operateType: OperateEnumType.Dialog,
      modelName: modelName,
      modelTreeData: modelTreeData
    };
    if (props.isParams && props.maxList === 1) {
      obj.apiParams = apiParams;
    }
    props.onChange(obj);
  }, [modelName, modelTreeData, apiParams]);
  function onChangeFn(v, treeData) {
    var num = getTreeDataNum(treeData);
    if (props.maxList && num > props.maxList) {
      return;
    }
    setModelName(v ? v : []);
    setModelTreeData(filter_source(treeData ? treeData : []));
    if (props.isParams && props.maxList === 1) {
      var data = getDialogParamsByTreeData(treeData);
      data = getApiParams(data);
      setApiParams(data);
    }
  }
  return /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: "10px"
    }
  }, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: props.title
  }, /*#__PURE__*/React.createElement(_CascaderSelect, {
    multiple: true,
    className: "itemOne_select",
    popupClassName: "itemOne-Select-popup",
    listClassName: "itemOne-Select-list",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    dataSource: dataSource,
    value: modelName,
    onChange: function onChange(v, treeData) {
      onChangeFn(v, treeData);
    },
    placeholder: t('setter.selectPlaceHolder')
  })), props.isParams && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "dataSourceTitle"
  }, t('setter.requestParams')), /*#__PURE__*/React.createElement("div", {
    className: "pramsBox requireBody"
  }, /*#__PURE__*/React.createElement(CTreeDataRelationSetter, {
    key: getNewKeyByApiParams(apiParams),
    value: apiParams || [],
    disabledInput: true,
    maxAddNum: -1,
    onChange: function onChange(e) {
      setApiParams(e ? e : []);
    }
  }))));
};