import { defineComponent, toRefs, reactive, watch, ref, onMounted, createVNode } from 'vue';
import { EnterOutlined } from '@ant-design/icons-vue';
import '../../../utils/index.mjs';
import { Textarea } from '../../input/index.mjs';
import useStyle from '../style/index.mjs';
import { editableProps } from './props.mjs';
import { KeyCode } from '../../../utils/KeyCode.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

var Editable = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "Editable",
  inheritAttrs: false,
  props: editableProps(),
  // emits: ['save', 'cancel', 'end', 'change'],
  setup(props, {
    emit,
    slots,
    attrs
  }) {
    const {
      prefixCls
    } = toRefs(props);
    const state = reactive({
      current: props.value || "",
      lastKeyCode: void 0,
      inComposition: false,
      cancelFlag: false
    });
    watch(() => props.value, (current) => {
      state.current = current;
    });
    const textArea = ref();
    onMounted(() => {
      var _a;
      if (textArea.value) {
        const resizableTextArea = (_a = textArea.value) == null ? void 0 : _a.resizableTextArea;
        const innerTextArea = resizableTextArea == null ? void 0 : resizableTextArea.textArea;
        innerTextArea.focus();
        const {
          length
        } = innerTextArea.value;
        innerTextArea.setSelectionRange(length, length);
      }
    });
    function saveTextAreaRef(node) {
      textArea.value = node;
    }
    function onChange({
      target: {
        value
      }
    }) {
      state.current = value.replace(/[\r\n]/g, "");
      emit("change", state.current);
    }
    function onCompositionStart() {
      state.inComposition = true;
    }
    function onCompositionEnd() {
      state.inComposition = false;
    }
    function onKeyDown(e) {
      const {
        keyCode
      } = e;
      if (keyCode === KeyCode.ENTER)
        e.preventDefault();
      if (state.inComposition)
        return;
      state.lastKeyCode = keyCode;
    }
    function onKeyUp(e) {
      const {
        keyCode,
        ctrlKey,
        altKey,
        metaKey,
        shiftKey
      } = e;
      if (state.lastKeyCode === keyCode && !state.inComposition && !ctrlKey && !altKey && !metaKey && !shiftKey) {
        if (keyCode === KeyCode.ENTER) {
          confirmChange();
          emit("end");
        } else if (keyCode === KeyCode.ESC) {
          state.current = props.originContent;
          emit("cancel");
        }
      }
    }
    function onBlur() {
      confirmChange();
    }
    function confirmChange() {
      emit("save", state.current.trim());
    }
    const [wrapSSR, hashId] = useStyle(prefixCls);
    return () => {
      const textAreaClassName = classNames({
        [`${prefixCls.value}`]: true,
        [`${prefixCls.value}-edit-content`]: true,
        [`${prefixCls.value}-rtl`]: props.direction === "rtl",
        [props.component ? `${prefixCls.value}-${props.component}` : ""]: true
      }, attrs.class, hashId.value);
      return wrapSSR(createVNode("div", {
        ...attrs,
        "class": textAreaClassName
      }, [createVNode(Textarea, {
        "ref": saveTextAreaRef,
        "maxlength": props.maxlength,
        "value": state.current,
        "onChange": onChange,
        "onKeydown": onKeyDown,
        "onKeyup": onKeyUp,
        "onCompositionstart": onCompositionStart,
        "onCompositionend": onCompositionEnd,
        "onBlur": onBlur,
        "rows": 1,
        "autoSize": props.autoSize === void 0 || props.autoSize
      }, null), slots.enterIcon ? slots.enterIcon({
        className: `${props.prefixCls}-edit-content-confirm`
      }) : createVNode(EnterOutlined, {
        "class": `${props.prefixCls}-edit-content-confirm`
      }, null)]));
    };
  }
});

export { Editable as default };
//# sourceMappingURL=Editable.mjs.map
