"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _drawer = _interopRequireDefault(require("@alifd/next/lib/drawer"));
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _react = _interopRequireWildcard(require("react"));
var _boxSlotComponent = _interopRequireDefault(require("../_components/boxSlotComponent"));
var _type = require("../cDataSource-setter/type");
var _locale = require("../../../src/locale");
require("./index.less");
var _ = require("..");
var _index2 = require("./commonFn/index");
var _cLoadingConfigSetter = _interopRequireDefault(require("../_components/cLoadingConfig-setter"));
var _cMessageConfigSetter = _interopRequireDefault(require("../_components/cMessageConfig-setter"));
var _renderByFormKey = _interopRequireDefault(require("../_components/renderByFormKey"));
var _formKeyType = require("../../_utils/formKeyType");
var _comIndex = require("../cDataSource-setter/commonFn/comIndex");
var _treeContext = require("./treeContext");
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; }
/**
 * 由于原生数组setter不支持多层弹框问题，故重新封装一个具备数组功能的组件
 */

// import { createDataSourceItem } from "../cDataSource-setter/commonFn/createDataSourceItem";

var CDataSourceListSetter = function CDataSourceListSetter(props) {
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)([]),
    dataSourceSelectList = _useState[0],
    setDataSourceSelectList = _useState[1];
  var _useState2 = (0, _react.useState)(-1),
    maxAddNum = _useState2[0],
    setMaxAddNum = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    paramsOpen = _useState3[0],
    setParamsOpen = _useState3[1];
  var _useState4 = (0, _react.useState)(value || []),
    dataSourceList = _useState4[0],
    setDataSourceList = _useState4[1];
  var _useState5 = (0, _react.useState)((0, _index2.getInitDrawerConfig)()),
    drawerConfig = _useState5[0],
    setDrawerConfig = _useState5[1];
  (0, _react.useEffect)(function () {
    var _props$field, _props$field$extraPro;
    setDataSourceSelectList((0, _index2.getOptionsByDataSourceList)());
    if (((_props$field = props.field) === null || _props$field === void 0 ? void 0 : (_props$field$extraPro = _props$field.extraProps) === null || _props$field$extraPro === void 0 ? void 0 : _props$field$extraPro.maxAddNum) !== undefined) {
      setMaxAddNum(props.field.extraProps.maxAddNum);
    } else if (props.maxAddNum !== undefined) {
      setMaxAddNum(props.maxAddNum);
    }
    var newDataSourceList = (0, _index2.updatedApiParamsByMainDataSource)(value);
    setDataSourceList(newDataSourceList);
  }, []);
  (0, _react.useEffect)(function () {
    onChange(dataSourceList);
  }, [dataSourceList]);
  function onChangeItemDataSource(data) {
    var newList = (0, _index2.alterDataSourceList)(data, dataSourceList);
    setDataSourceList(newList);
  }
  function handleDataList(type, id) {
    var newArr = getOldArrData();
    if (type === "add") {
      newArr.push((0, _index2.getCreateDataSourceItem)());
    } else if (id !== undefined) {
      newArr = newArr.filter(function (item) {
        return item.id !== id;
      });
    }
    setDataSourceList([].concat(newArr));
  }
  function getOldArrData() {
    var result = [];
    return result.concat(dataSourceList);
  }
  function pactAddressChange(data) {
    var obj = drawerConfig.data;
    if (drawerConfig.data.pactAddress) {
      var newData = (0, _comIndex.getChangePactAddressObj)(data, drawerConfig.data.pactAddress);
      if (newData) {
        obj.pactAddress = newData;
        onChangeItemDataSource(obj);
      }
    }
  }
  function dataSourceListChange(dataSourceId, option, id) {
    var obj = {
      id: id,
      dataSourceId: dataSourceId ? dataSourceId : "",
      dataSourceName: option ? option.label : "",
      apiParams: dataSourceId ? (0, _index2.getApiParamsByDataSourceId)(dataSourceId) : [],
      loadingConfig: {
        openType: false,
        openMode: _type.LoadingConfigOpenMode["default"]
      },
      messageConfig: {
        openType: false,
        modeTypeList: [],
        contentList: []
      }
    };
    var res = (0, _index2.getDataSourceDataById)(dataSourceId);
    if (res !== null && res !== void 0 && res.selectValue && res !== null && res !== void 0 && res.pactAddress) {
      obj.pactAddress = (0, _index2.getApiParams)(res.pactAddress)[0];
      obj.selectValue = res.selectValue;
    }
    return obj;
  }
  var isShowAddIcon = function isShowAddIcon() {
    var result = true;
    if (maxAddNum !== -1 && dataSourceList.length >= maxAddNum) {
      result = false;
    }
    return result;
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "CDataSourceListSetterBox"
  }, dataSourceList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "cDataItemOne"
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      style: {
        cursor: "pointer"
      },
      type: "edit",
      onClick: function onClick() {
        setDrawerConfig({
          index: index,
          visible: true,
          data: item
        });
      }
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "CDataItemSelectBox"
    }, index === 0 && /*#__PURE__*/_react["default"].createElement("div", {
      className: "CDataItemIdName"
    }, (0, _locale.t)("setter.dataId")), /*#__PURE__*/_react["default"].createElement(_select["default"], {
      hasClear: true,
      value: item.dataSourceId,
      dataSource: dataSourceSelectList,
      placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
      style: {
        width: "100%"
      },
      onChange: function onChange(e, type, option) {
        var obj = dataSourceListChange(e, option, item.id);
        obj.apiParams = (0, _index2.getApiParams)(obj.apiParams);
        onChangeItemDataSource(obj);
      }
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "CDataBtn"
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      style: {
        cursor: "pointer"
      },
      type: "ashbin",
      onClick: function onClick() {
        handleDataList("remove", item.id);
      }
    })));
  }), isShowAddIcon() && /*#__PURE__*/_react["default"].createElement("div", {
    className: "sub_btn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    text: true,
    type: "primary",
    onClick: function onClick() {
      handleDataList("add");
    }
  }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _locale.t)("setter.addData")))), /*#__PURE__*/_react["default"].createElement(_drawer["default"], {
    title: (0, _locale.t)("setter.dataAlter"),
    width: 500,
    placement: "right",
    visible: drawerConfig.visible,
    headerStyle: {
      position: "fixed",
      width: "500px",
      zIndex: 999
    },
    bodyStyle: {
      paddingTop: "50px"
    },
    onClose: function onClose() {
      setDrawerConfig((0, _index2.getInitDrawerConfig)());
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "CDataDItemBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.dataId")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    hasClear: true,
    value: drawerConfig.data.dataSourceId,
    dataSource: dataSourceSelectList,
    style: {
      width: "100%"
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    onChange: function onChange(e, type, option) {
      var obj = dataSourceListChange(e, option, drawerConfig.data.id);
      obj.apiParams = (0, _index2.getApiParams)(obj.apiParams);
      onChangeItemDataSource(obj);
      setDrawerConfig((0, _extends2["default"])({}, drawerConfig, {
        data: obj
      }));
    }
  }))), drawerConfig.data !== null && /*#__PURE__*/_react["default"].createElement("div", {
    className: "CDataDItemBox"
  }, drawerConfig.data.selectValue === _type.DialogConfigType.PACT && drawerConfig.data.pactAddress && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.pathAddress")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: (0, _formKeyType.getRelationList)(),
    value: drawerConfig.data.pactAddress,
    onChange: pactAddressChange
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "dataSourceTitle",
    style: {
      display: 'flex',
      justifyContent: 'space-between'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "title"
  }, (0, _locale.t)('setter.requestParams')), /*#__PURE__*/_react["default"].createElement("div", null, (0, _locale.t)('setter.advanced'), ":", /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    checked: paramsOpen,
    onChange: function onChange(v) {
      setParamsOpen(v);
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "pramsBox requireBody"
  }, /*#__PURE__*/_react["default"].createElement(_treeContext.CTreeContext.Provider, {
    value: {
      paramsOpen: paramsOpen
    }
  }, /*#__PURE__*/_react["default"].createElement(_.CTreeDataRelationSetter, {
    key: drawerConfig.data.id + "_" + drawerConfig.data.dataSourceId,
    value: drawerConfig.data.apiParams // apiParams必须同步远端数据源
    ,
    maxAddNum: -1,
    onChange: function onChange(e) {
      var obj = drawerConfig.data;
      obj.apiParams = e;
      onChangeItemDataSource(obj);
    }
  }))), drawerConfig.data.loadingConfig && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.loadingSet"),
    labelAlign: "top"
  }, /*#__PURE__*/_react["default"].createElement(_cLoadingConfigSetter["default"], {
    value: drawerConfig.data.loadingConfig,
    onChange: function onChange(e) {
      var obj = drawerConfig.data;
      obj.loadingConfig = e;
      onChangeItemDataSource(obj);
    }
  })), drawerConfig.data.messageConfig && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.tipSet"),
    labelAlign: "top"
  }, /*#__PURE__*/_react["default"].createElement(_cMessageConfigSetter["default"], {
    value: drawerConfig.data.messageConfig,
    tipOptions: [{
      label: (0, _locale.t)("setter.tipError"),
      value: _type.MessageModeType.error
    }, {
      label: (0, _locale.t)("setter.tipSuccess"),
      value: _type.MessageModeType.success
    }],
    onChange: function onChange(e) {
      var obj = drawerConfig.data;
      obj.messageConfig = e;
      onChangeItemDataSource(obj);
    }
  })))));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CDataSourceListSetter, this.props);
  };
  return _default;
}(_react.Component);