"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.StorageDataComponent = void 0;
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../type");
var _locale = require("../../../../src/locale");
var _boxSlotComponent = _interopRequireDefault(require("../../_components/boxSlotComponent"));
var _config = require("../config");
var _utils = require("../config/utils");
var _formKeyType = require("../../../_utils/formKeyType");
var _renderByFormKey = _interopRequireDefault(require("../../_components/renderByFormKey"));
var _utils2 = require("../../../_utils/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 StorageDataComponent = exports.StorageDataComponent = function StorageDataComponent(props) {
  var _saveData$localConfig;
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(value || {
      type: _type.StorageDataBType.Local,
      localConfig: {
        type: _type.StorageLocalType.LocalStorage,
        key: (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, ''),
        value: (0, _utils.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '')
      }
    }),
    saveData = _useState[0],
    setSaveData = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    comKeyOptions = _useState2[0],
    setKeyComOptions = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    comOptions = _useState3[0],
    setComOptions = _useState3[1];
  (0, _react.useEffect)(function () {
    setComOptions((0, _formKeyType.getRelationList)());
    setKeyComOptions((0, _formKeyType.getDefaultTypeRelationList)());
  }, []);
  (0, _react.useEffect)(function () {
    onChange(saveData);
  }, [saveData]);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.saveDataTitle")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: saveData.type,
    dataSource: (0, _config.getSaveDataBehaviorTypeList)(),
    onChange: function onChange(e) {
      setSaveData((0, _extends2["default"])({}, saveData, {
        type: e
      }));
    }
  })), saveData.type === _type.StorageDataBType.Local && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.saveLocalTitle")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: ((_saveData$localConfig = saveData.localConfig) === null || _saveData$localConfig === void 0 ? void 0 : _saveData$localConfig.type) || _type.StorageLocalType.LocalStorage,
    dataSource: (0, _config.getSaveLocalTypeList)(),
    onChange: function onChange(e) {
      setSaveData((0, _extends2["default"])({}, saveData, {
        localConfig: (0, _extends2["default"])({}, saveData.localConfig, {
          type: e
        })
      }));
    }
  })), saveData.type === _type.StorageDataBType.OnLine && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.saveOnLineTitle")
  }, /*#__PURE__*/_react["default"].createElement("div", null, "developing")), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.saveLocalKey")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: comKeyOptions,
    value: saveData.localConfig.key,
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, saveData.localConfig.key, newItem);
        setSaveData((0, _extends2["default"])({}, saveData, {
          localConfig: (0, _extends2["default"])({}, saveData.localConfig, {
            key: newData
          })
        }));
      });
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.saveLocalValue")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: comOptions,
    value: saveData.localConfig.value,
    onChange: function onChange(v) {
      (0, _utils2.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, saveData.localConfig.value, newItem);
        setSaveData((0, _extends2["default"])({}, saveData, {
          localConfig: (0, _extends2["default"])({}, saveData.localConfig, {
            value: newData
          })
        }));
      });
    }
  })));
};