"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 _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _cascaderSelect = _interopRequireDefault(require("@alifd/next/lib/cascader-select"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _grid = _interopRequireDefault(require("@alifd/next/lib/grid"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _lodash = require("lodash");
var _utils = require("../../_utils/utils");
var _formKeyType = require("../../_utils/formKeyType");
var _type = require("../../_utils/treeDataRelationFn/type");
var _comFn = require("../cTreeDataRelation-setter/comFn");
var _tupleRender = _interopRequireDefault(require("./tupleRender"));
var _locale = require("../../../src/locale");
var _cJsonDomSetter = _interopRequireDefault(require("../cJsonDom-setter"));
var _jsonDomSelectService = require("../../_utils/jsonDomSelectService");
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; }
var Option = _select["default"].Option;
var Row = _grid["default"].Row,
  Col = _grid["default"].Col;
var RenderByFormKeyPage = function RenderByFormKeyPage(props) {
  var value = props.value,
    onChange = props.onChange;
  var jsonDomSelectService = new _jsonDomSelectService.JsonDomSelectService();
  var _useState = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.formKey) || _formKeyType.DataSourceType.defaultType),
    formKey = _useState[0],
    setFormKey = _useState[1];
  var _useState2 = (0, _react.useState)(value === null || value === void 0 ? void 0 : value.data),
    data = _useState2[0],
    setData = _useState2[1];
  var _useState3 = (0, _react.useState)(props.relationOption),
    relationOption = _useState3[0],
    setRelationOption = _useState3[1];
  var _useState4 = (0, _react.useState)(false),
    showJsonDom = _useState4[0],
    setShowJsonDom = _useState4[1];
  var _useState5 = (0, _react.useState)(''),
    jsonSelectValue = _useState5[0],
    setJsonSelectValue = _useState5[1];
  var _useState6 = (0, _react.useState)({}),
    renderJson = _useState6[0],
    setRenderJson = _useState6[1];
  var _useState7 = (0, _react.useState)(false),
    dialogVisible = _useState7[0],
    setDialogVisible = _useState7[1];
  var _useState8 = (0, _react.useState)(false),
    queryLoading = _useState8[0],
    setQueryLoading = _useState8[1];
  var _useState9 = (0, _react.useState)([]),
    currentPageData = _useState9[0],
    setCurrentPageData = _useState9[1];
  var _useState10 = (0, _react.useState)([]),
    treeDataSource = _useState10[0],
    setTreeDataSource = _useState10[1];
  (0, _react.useEffect)(function () {
    getRelationOption();
    // changeTreeDataSource(formKey);
  }, []);
  (0, _react.useEffect)(function () {
    changeTreeDataSource(formKey);
  }, [formKey]);
  (0, _react.useEffect)(function () {
    var res = jsonDomSelectService.showEditIcon(formKey, data);
    setShowJsonDom(res);
  }, [formKey, data]);
  (0, _react.useEffect)(function () {
    getRelationOption();
  }, [props.relationOption]);
  var getInputShowState = function getInputShowState() {
    var result = formKey !== _formKeyType.DataSourceType.dictionaryType;

    // 选中数据源和自定义变量时，需要特殊处理一下
    if (formKey === _formKeyType.DataSourceType.dataSourceType || formKey === _formKeyType.DataSourceType.customStateType) {
      result = showJsonDom;
    }
    return result;
  };
  function queryDataSource() {
    setQueryLoading(true);
    jsonDomSelectService.getJsonRenderData(formKey, data).then(function (res) {
      setJsonSelectValue(res.jsonSelectValue);
      setRenderJson(res.renderJson);
      setQueryLoading(false);
      setDialogVisible(true);
    });
  }
  function getRelationOption() {
    if (formKey === _formKeyType.DataSourceType.tupleType) {
      var list = (0, _formKeyType.getTupleTypeRelationList)();
      setRelationOption([].concat(relationOption, list));
    } else {
      setRelationOption(props.relationOption);
    }
  }
  function onComChange(list) {
    onChange(list);
  }
  function changeTreeDataSource(key) {
    var _obj;
    if (!key) {
      return;
    }
    var obj = (_obj = {}, _obj[_formKeyType.DataSourceType.currentPageType] = _comFn.getPageTreeDataRelation, _obj[_formKeyType.DataSourceType.dataSourceType] = _comFn.getDataSourceDataRelation, _obj[_formKeyType.DataSourceType.customStateType] = _comFn.getCustomStateRelation, _obj[_formKeyType.DataSourceType.modalDialogType] = _comFn.getModalDialogRelation, _obj[_formKeyType.DataSourceType.currentWalletConnectType] = _comFn.getWalletConnectRelation, _obj[_formKeyType.DataSourceType.dictionaryType] = _comFn.getDictionaryRelation, _obj[_formKeyType.DataSourceType.engineRuleType] = _comFn.getEngineRuleRelation, _obj[_formKeyType.DataSourceType.loopDataSelectedType] = _comFn.getLoopDataSelectedRelation, _obj[_formKeyType.DataSourceType.locationType] = _comFn.getUrlRelation, _obj[_formKeyType.DataSourceType.storageDataType] = _comFn.getStorageDataRelation, _obj);
    var fn = obj[key];
    if (fn) {
      var _data = fn();
      if (key === _formKeyType.DataSourceType.currentPageType) {
        setCurrentPageData(_data);
      } else {
        setTreeDataSource(_data);
      }
    }
  }

  /**
   * formKey改变
   * @param v
   */
  function onFormKeyChange(v) {
    setFormKey(v);
    var initData = getInitDataByFormKey(v);
    setData(initData);
    onComChange([{
      key: "formKey",
      value: v
    }, {
      key: "data",
      value: ""
    }, {
      key: "treeData",
      value: []
    }]);
  }
  function childrenChange(v, treeData) {
    setData(v);
    onComChange([{
      key: "data",
      value: v
    }, {
      key: "treeData",
      value: (0, _lodash.isArray)(treeData) ? treeData : [(0, _utils.filter_source)(treeData)]
    }]);
  }
  function onChangeDataSource(v, index, treeData) {
    var oldData = (0, _lodash.isArray)(data) ? (0, _lodash.cloneDeep)(data) : ["", ""];
    oldData[index] = v;
    var comList = [{
      key: "data",
      value: oldData
    }];
    if (treeData) {
      comList.push({
        key: "treeData",
        value: (0, _lodash.isArray)(treeData) ? treeData : [(0, _utils.filter_source)(treeData)]
      });
    }
    setData(oldData);
    onComChange(comList);
  }
  function getInitDataByFormKey(v) {
    var result = "";
    if (v === _formKeyType.DataSourceType.dataSourceType) {
      result = ["", ""];
    } else if (v === _formKeyType.DataSourceType.pactFunctionType) {
      result = {
        pactName: "",
        pactAddress: "",
        pactMethod: "",
        pactParams: []
      };
    }
    return result;
  }
  function tupleRenderOnChange(list) {
    var data = [];
    if (list && list.length) {
      list.forEach(function (item) {
        if (item.key === "data") {
          data = item.value;
        }
      });
    }
    setData(data);
    onComChange(list);
  }
  var ComInputMemo = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "dataSourceRowItem flexComWidth"
    }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
      hasClear: !value.disabled,
      disabled: value.disabled,
      value: data && data[1] || "",
      style: {
        width: "100%"
      },
      placeholder: (0, _locale.t)("setter.inputPlaceHolder"),
      onChange: function onChange(v) {
        onChangeDataSource(v, 1);
      }
    }), showJsonDom && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: queryDataSource
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: queryLoading ? 'loading' : "edit"
    })), /*#__PURE__*/_react["default"].createElement(_cJsonDomSetter["default"], {
      showType: "none",
      dialogVisible: dialogVisible,
      value: jsonSelectValue,
      dataSource: renderJson,
      onClose: function onClose() {
        setDialogVisible(false);
      },
      onChange: function onChange(v) {
        setJsonSelectValue(v);
        onChangeDataSource(v, 1);
      }
    })));
  }, [showJsonDom, queryLoading, dialogVisible, jsonSelectValue, renderJson, data, value.disabled]);
  var ComCascaderSelectMemo = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "flexComWidth"
    }, /*#__PURE__*/_react["default"].createElement(_cascaderSelect["default"], {
      className: "itemOne_select",
      placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
      popupClassName: "itemOne-Select-popup",
      listClassName: "itemOne-Select-list",
      style: {
        width: '100%',
        minWidth: 0
      },
      dataSource: treeDataSource,
      disabled: value.disabled,
      value: data && data[0] || "",
      onChange: function onChange(v, treeData) {
        onChangeDataSource(v, 0, treeData);
      }
    }));
  }, [treeDataSource, data, value.disabled]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "renderByFormKeyBox"
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    autoWidth: false,
    popupClassName: "itemOne-Select-formKey-popup",
    style: {
      width: '100%',
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    disabled: formKey === _formKeyType.DataSourceType.tupleType || value.disabled,
    dataSource: relationOption,
    value: formKey || _formKeyType.DataSourceType.currentPageType,
    onChange: function onChange(v) {
      onFormKeyChange(v);
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "renderByFormKeyBox_Other"
  }, formKey === _formKeyType.DataSourceType.currentPageType && /*#__PURE__*/_react["default"].createElement(_cascaderSelect["default"], {
    className: "itemOne_select",
    popupClassName: "itemOne-Select-popup",
    listClassName: "itemOne-Select-list",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    dataSource: currentPageData,
    disabled: value.disabled,
    value: data,
    onChange: function onChange(v, treeData) {
      childrenChange(v, treeData);
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder")
  }), formKey === _formKeyType.DataSourceType.defaultType && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, value.inputType === _formKeyType.DefaultInputType.select && /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)('SettingPanel.placeholderSelect'),
    notFoundContent: (0, _locale.t)('SettingPanel.notFoundContent'),
    disabled: value.disabled,
    dataSource: value.options,
    value: data,
    onChange: function onChange(v) {
      childrenChange(v, []);
    }
  }), value.inputType === _formKeyType.DefaultInputType["boolean"] && /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    disabled: value.disabled,
    defaultChecked: data,
    onChange: function onChange(v) {
      childrenChange(v, []);
    }
  }), value.inputType !== _formKeyType.DefaultInputType["boolean"] && value.inputType !== _formKeyType.DefaultInputType.select && /*#__PURE__*/_react["default"].createElement(_input["default"], {
    hasClear: !value.disabled,
    disabled: value.disabled,
    value: data,
    style: {
      width: 100 + "%"
    },
    placeholder: (0, _locale.t)("setter.inputPlaceHolder"),
    onChange: function onChange(v) {
      childrenChange(v, []);
    }
  })), (formKey === _formKeyType.DataSourceType.requestType || formKey === _formKeyType.DataSourceType.loopDataType || formKey === _formKeyType.DataSourceType.renderPropsDataType) && /*#__PURE__*/_react["default"].createElement(_input["default"], {
    hasClear: !value.disabled,
    disabled: value.disabled,
    value: data,
    style: {
      width: 100 + "%"
    },
    placeholder: (0, _locale.t)("setter.inputPlaceHolder"),
    onChange: function onChange(v) {
      childrenChange(v, []);
    }
  }), (formKey === _formKeyType.DataSourceType.dataSourceType || formKey === _formKeyType.DataSourceType.customStateType || formKey === _formKeyType.DataSourceType.modalDialogType || formKey === _formKeyType.DataSourceType.dictionaryType || formKey === _formKeyType.DataSourceType.loopDataSelectedType || formKey === _formKeyType.DataSourceType.storageDataType) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "dataSourceRowItem"
  }, ComCascaderSelectMemo, getInputShowState() && ComInputMemo), formKey === _formKeyType.DataSourceType.engineRuleType && /*#__PURE__*/_react["default"].createElement("div", {
    className: "dataSourceRowItem"
  }, ComCascaderSelectMemo, data[0] && data[0].length && data[0].endsWith('.data') && ComInputMemo), (formKey === _formKeyType.DataSourceType.locationType || formKey === _formKeyType.DataSourceType.currentWalletConnectType) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "dataSourceRowItem"
  }, ComCascaderSelectMemo, (data[0] === _type.WalletConnectType.Custom || data[0] === _type.LocationUrlType.custom) && ComInputMemo), formKey === _formKeyType.DataSourceType.tupleType && value.components && /*#__PURE__*/_react["default"].createElement(_tupleRender["default"], {
    addable: value.addable || 0,
    data: data || [],
    components: value.components,
    onChange: tupleRenderOnChange
  })));
};
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(RenderByFormKeyPage, this.props);
  };
  return _default;
}(_react.Component);