"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.AwaitComponent = void 0;
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../type");
var _boxSlotComponent = _interopRequireDefault(require("../../_components/boxSlotComponent"));
var _cLoadingConfigSetter = _interopRequireDefault(require("../../_components/cLoadingConfig-setter"));
var _type2 = require("../../cDataSource-setter/type");
var _renderByFormKey = _interopRequireDefault(require("../../_components/renderByFormKey"));
var _locale = require("../../../../src/locale");
var _formKeyType = require("../../../_utils/formKeyType");
var _utils = require("../../../_utils/utils");
var _utils2 = require("../config/utils");
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 AwaitOptions = [{
  label: (0, _locale.t)("setter.time"),
  value: _type.AwaitBehaViorOption.Time
}, {
  label: (0, _locale.t)("setter.block"),
  value: _type.AwaitBehaViorOption.Block
}];
var AwaitComponent = exports.AwaitComponent = function AwaitComponent(props) {
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.type) || _type.AwaitBehaViorOption.Time),
    type = _useState[0],
    setType = _useState[1];
  var _useState2 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.data) || (0, _utils2.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '')),
    data = _useState2[0],
    setData = _useState2[1];
  var _useState3 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.loadingConfig) || {
      openType: false,
      openMode: _type2.LoadingConfigOpenMode["default"]
    }),
    loadingConfig = _useState3[0],
    setLoadingConfig = _useState3[1];
  var _useState4 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.outputConfig) || {
      openType: false,
      stateKey: (0, _utils2.getInitStateKey)(_formKeyType.DataSourceType.customStateType, '')
    }),
    outputConfig = _useState4[0],
    setOutputConfig = _useState4[1];
  var _useState5 = (0, _react.useState)([]),
    awaitRelationOption = _useState5[0],
    setAwaitRelationOption = _useState5[1];
  var _useState6 = (0, _react.useState)([]),
    relationOption = _useState6[0],
    setRelationOption = _useState6[1];
  (0, _react.useEffect)(function () {
    setAwaitRelationOption((0, _formKeyType.getRelationList)());
    setRelationOption((0, _formKeyType.getCustomStateTypeRelationList)());
  }, []);
  (0, _react.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 (0, _utils2.getInitStateKey)(_formKeyType.DataSourceType.defaultType, data);
    }
    return data;
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "awaitComponentBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.awaitType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: AwaitOptions,
    value: type,
    onChange: function onChange(v) {
      setType(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: type === _type.AwaitBehaViorOption.Time ? (0, _locale.t)("setter.awaitTime") + "(s)" : (0, _locale.t)("setter.awaitBlock") + "(block)"
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: awaitRelationOption,
    value: handleOldData(data),
    onChange: function onChange(v) {
      (0, _utils.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, data, newItem);
        setData(newData);
      });
    }
  }))), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: "loading",
    labelAlign: "top"
  }, /*#__PURE__*/_react["default"].createElement(_cLoadingConfigSetter["default"], {
    value: loadingConfig,
    onChange: function onChange(e) {
      setLoadingConfig(e);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.awaitCustomStateOpen")
  }, /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    checked: outputConfig.openType,
    onChange: function onChange(v) {
      setOutputConfig((0, _extends2["default"])({}, outputConfig, {
        openType: v
      }));
    }
  })), outputConfig.openType && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.awaitCustomState")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: relationOption,
    value: outputConfig.stateKey,
    onChange: function onChange(v) {
      (0, _utils.handleRenderByFormKeyData)(v, function (newItem) {
        var newCopy = (0, _extends2["default"])({}, outputConfig.stateKey, newItem);
        setOutputConfig((0, _extends2["default"])({}, outputConfig, {
          stateKey: newCopy
        }));
      });
    }
  })));
};