"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _utils = 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; }
// InputLang.tsx

var Option = _select["default"].Option;
var InputLang = function InputLang(_ref) {
  var onChange = _ref.onChange,
    value = _ref.value;
  var langList = [{
    value: 'en',
    label: 'En'
  }, {
    value: 'zh',
    label: '中文'
  }, {
    value: 'zh_tw',
    label: '中文(繁体)'
  }];
  var _useState = (0, _react.useState)((0, _utils.processField)(value || '')),
    content = _useState[0],
    setContent = _useState[1];
  var _useState2 = (0, _react.useState)(localStorage.getItem('lang') || 'en'),
    contentLang = _useState2[0],
    setContentLang = _useState2[1];

  // 更新 content 时，设置对应语言的值
  var handleContentChange = function handleContentChange(value) {
    var updatedContent = content;
    updatedContent[contentLang] = value;
    setContent(updatedContent);
    // 将更新后的内容传递给父组件
    onChange(JSON.stringify(updatedContent));
  };
  var handleBlur = function handleBlur() {
    var updatedContent = (0, _extends2["default"])({}, content);
    var data = (0, _utils.assignValues)(updatedContent);
    setContent(data); // Update content after assignValues
    onChange(JSON.stringify(data)); // Notify parent component of the updated value
  };

  // 切换语言时，设置当前语言
  var handleLanguageChange = function handleLanguageChange(value) {
    setContentLang(value);
  };

  // 更新 content 数据
  _react["default"].useEffect(function () {
    setContent((0, _utils.processField)(value || ''));
  }, [value]);
  var select = /*#__PURE__*/_react["default"].createElement(_select["default"], {
    value: contentLang,
    onChange: function onChange(value) {
      return handleLanguageChange(value);
    },
    popupStyle: {
      width: 150
    }
  }, langList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(Option, {
      key: index,
      value: item.value
    }, item.label);
  }));
  return /*#__PURE__*/_react["default"].createElement(_input["default"], {
    addonBefore: select,
    value: content[contentLang],
    onChange: function onChange(e) {
      return handleContentChange(e);
    },
    onBlur: handleBlur
  });
};
var _default = exports["default"] = InputLang;