import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Dialog from "@alifd/next/es/dialog";
import _Select from "@alifd/next/es/select";
import _Button from "@alifd/next/es/button";
import _extends from "@babel/runtime/helpers/extends";
import React, { Component, useState, useContext } from "react";
import CTreeDataRelationSetter from "../cTreeDataRelation-setter";
import { cloneDeep } from "lodash";
import { compatibleTupleData, getTupleOriginOptions, handleRenderByFormKeyData, uuid } from "../../_utils/utils";
import { getInitStateKey, getNewKeyByApiParams } from "../cBehavior-setter/config/utils";
import "./index.less";
import { t } from "../../../src/locale";
import { CTreeContext } from "../cDataSourceList-setter/treeContext";
import { DataSourceType, getRelationList, TupleOriginType } from "../../_utils/formKeyType";
import BoxSlotComponent from "./boxSlotComponent";
import RenderByFormKey from "./renderByFormKey";
var TupleRender = function TupleRender(props) {
  var onChange = props.onChange,
    components = props.components,
    data = props.data,
    addable = props.addable;
  var CTrCxt = useContext(CTreeContext);
  var _useState = useState({
      visible: false,
      data: {
        originType: TupleOriginType["default"],
        defaultData: [],
        customData: getInitStateKey(DataSourceType.defaultType, '')
      }
    }),
    dialogConfig = _useState[0],
    setDialogConfig = _useState[1];
  function closeDialog() {
    setDialogConfig(_extends({}, dialogConfig, {
      visible: false
    }));
  }
  function handleOk() {
    var listData = [{
      key: "components",
      value: components
    }, {
      key: "data",
      value: dialogConfig.data || {}
    }, {
      key: "addable",
      value: addable || 0
    }];
    onChange(listData);
    closeDialog();
  }
  function onChangeTuple(e, index) {
    var oldDefaultData = cloneDeep(dialogConfig.data.defaultData || []);
    var newDefaultData = oldDefaultData.map(function (item, i) {
      if (i === index) {
        return e;
      }
      return item;
    });
    setDialogConfig(_extends({}, dialogConfig, {
      data: _extends({}, dialogConfig.data, {
        defaultData: newDefaultData
      })
    }));
  }
  function addSelectOne() {
    var base = cloneDeep(components).map(function (item) {
      item.id = uuid();
      return item;
    });
    if (dialogConfig.data.defaultData) {
      var newData = cloneDeep(dialogConfig.data.defaultData).concat([base]);
      setDialogConfig(_extends({}, dialogConfig, {
        data: _extends({}, dialogConfig.data, {
          defaultData: newData
        })
      }));
    }
  }
  function removeData(index) {
    if (dialogConfig.data.defaultData) {
      var _data = cloneDeep(dialogConfig.data.defaultData);
      _data.splice(index, 1);
      setDialogConfig(_extends({}, dialogConfig, {
        data: _extends({}, dialogConfig.data, {
          defaultData: _data
        })
      }));
    }
  }
  function isShowAddBtn() {
    return CTrCxt.isShowAddBtn && (components === null || components === void 0 ? void 0 : components.length) > 0 && dialogConfig.data.originType === TupleOriginType["default"] && (addable === 0 || dialogConfig.data.defaultData.length < addable);
  }
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Button, {
    disabled: components.length === 0,
    onClick: function onClick() {
      var newData = compatibleTupleData(data);
      setDialogConfig({
        visible: true,
        data: cloneDeep(newData)
      });
    }
  }, t("setter.setTuple")), /*#__PURE__*/React.createElement(_Dialog, {
    className: "CRenderBox-dialog",
    title: t("setter.setTuple"),
    footerAlign: "right",
    height: 650,
    visible: dialogConfig.visible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/React.createElement(_Button, {
      onClick: handleOk,
      type: "primary"
    }, t('setter.ok')), /*#__PURE__*/React.createElement(_Button, {
      style: {
        marginLeft: '10px'
      },
      onClick: closeDialog
    }, t('setter.cancel'))]
  }, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.tupleOriginType")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: getTupleOriginOptions(),
    value: dialogConfig.data.originType,
    onChange: function onChange(v) {
      setDialogConfig(_extends({}, dialogConfig, {
        data: _extends({}, dialogConfig.data, {
          originType: v
        })
      }));
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "tupleBox"
  }, dialogConfig.data.originType === TupleOriginType["default"] && dialogConfig.data.defaultData.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      className: "tupleBoxItem"
    }, /*#__PURE__*/React.createElement("div", {
      className: "tupleBoxChildren"
    }, /*#__PURE__*/React.createElement(CTreeDataRelationSetter, _extends({}, props, {
      key: getNewKeyByApiParams(item),
      value: item,
      maxAddNum: -1,
      onChange: function onChange(e) {
        onChangeTuple(e, index);
      }
    }))));
  }), dialogConfig.data.originType === TupleOriginType.customData && /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.tupleOriginCustomData")
  }, /*#__PURE__*/React.createElement(RenderByFormKey, {
    relationOption: getRelationList(),
    value: dialogConfig.data.customData,
    onChange: function onChange(v) {
      handleRenderByFormKeyData(v, function (newItem) {
        var newData = _extends({}, dialogConfig.data.customData, newItem);
        setDialogConfig(_extends({}, dialogConfig, {
          data: _extends({}, dialogConfig.data, {
            customData: newData
          })
        }));
      });
    }
  }))), isShowAddBtn() && /*#__PURE__*/React.createElement("div", {
    className: "tupleSubBtn"
  }, /*#__PURE__*/React.createElement(_Button, {
    text: true,
    type: "primary",
    onClick: addSelectOne
  }, /*#__PURE__*/React.createElement("span", null, t("setter.addData"))))));
};
var _default = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(TupleRender, this.props);
  };
  return _default;
}(Component);
export { _default as default };