var _excluded = ["value", "children", "editable", "onChange", "className", "size"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useState, useMemo } from 'react';
import { Input } from 'antd';
import Icon from "../../../icon";
import { locales } from '@pisell/utils';
import classNames from 'classnames';
import useEngineContext from "../../../../hooks/useEngineContext";
import Translation from "../../../translation";
import "./index.less";
var Editable = function Editable(props) {
  var value = props.value,
    children = props.children,
    _props$editable = props.editable,
    editable = _props$editable === void 0 ? false : _props$editable,
    onChange = props.onChange,
    className = props.className,
    _props$size = props.size,
    size = _props$size === void 0 ? 'middle' : _props$size,
    others = _objectWithoutProperties(props, _excluded);
  var context = useEngineContext();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isEditing = _useState2[0],
    setIsEditing = _useState2[1];
  // 解析编辑配置
  var editableConfig = useMemo(function () {
    if (typeof editable === 'boolean') {
      return editable ? {} : null;
    }
    return editable || null;
  }, [editable]);
  var currentLocale = useMemo(function () {
    var _context$appHelper$ut;
    var locale = (_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get('umi_locale');
    return locales.formatLocale(locale);
  }, []);
  var displayValue = useMemo(function () {
    if (typeof children === 'string') return children;
    if (typeof value === 'string') {
      return value;
    }
    if (value && _typeof(value) === 'object') {
      return value[currentLocale] || '';
    }
    return '';
  }, [value, currentLocale, children]);

  // 判断是否为多语言对象
  var isMultiLanguage = useMemo(function () {
    return !!value && _typeof(value) === 'object';
  }, [value]);

  // 编辑功能
  var handleEdit = function handleEdit() {
    if (editableConfig !== null && editableConfig !== void 0 && editableConfig.disabled) return;
    setIsEditing(true);
  };
  var handleInputChange = function handleInputChange(newValue) {
    if (!onChange) return;
    onChange(newValue);
  };

  // 编辑模式渲染
  if (isEditing && editableConfig) {
    // 如果是多语言对象，使用 Translation 组件
    if (isMultiLanguage) {
      return /*#__PURE__*/React.createElement("div", {
        className: classNames('pisell-text-editable-editing', className)
      }, /*#__PURE__*/React.createElement(Translation, {
        value: value,
        onChange: handleInputChange,
        onPressEnter: function onPressEnter() {
          return setIsEditing(false);
        }
        // onBlur={() => setIsEditing(false)}
        ,
        maxLength: editableConfig.maxLength,
        size: size,
        autoFocus: true
      }));
    }

    // 普通字符串，使用 Input 组件
    return /*#__PURE__*/React.createElement(Input, {
      value: displayValue,
      onChange: function onChange(e) {
        return handleInputChange(e.target.value);
      },
      onPressEnter: function onPressEnter() {
        return setIsEditing(false);
      }
      // onBlur={() => setIsEditing(false)}
      ,
      maxLength: editableConfig.maxLength,
      size: size,
      autoFocus: true,
      className: classNames('pisell-text-editable-input', className),
      style: others.style
    });
  }

  // 正常显示模式
  return /*#__PURE__*/React.createElement("span", _extends({
    className: classNames('pisell-text-editable', className, {
      'pisell-text-editable-enabled': editableConfig && !editableConfig.disabled
    })
  }, others), displayValue, editableConfig && !editableConfig.disabled && !isEditing && /*#__PURE__*/React.createElement("span", {
    className: "pisell-text-editable-edit-icon",
    onClick: function onClick(e) {
      e.preventDefault();
      e.stopPropagation();
      handleEdit();
    },
    title: "\u70B9\u51FB\u7F16\u8F91"
  }, editableConfig.icon || /*#__PURE__*/React.createElement(Icon, {
    type: "pisell2-edit-05",
    size: 16
  })));
};
export default Editable;