import { defineComponent, reactive, ref, computed, onMounted, onBeforeUnmount, watch, nextTick, watchEffect, createVNode, Fragment, toRaw } from 'vue';
import { EditOutlined, CheckOutlined, CopyOutlined } from '@ant-design/icons-vue';
import '../../../utils/index.mjs';
import '../../../vue-components/index.mjs';
import '../../../hooks/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import { Tooltip } from '../../tooltip/index.mjs';
import LocaleReceiver from '../../locale-provider/src/LocaleReceiver.mjs';
import Editable from './Editable.mjs';
import measure from './util.mjs';
import Typography from './Typography.mjs';
import { baseProps } from './props.mjs';
import copy from './copy/index.mjs';
import { isStyleSupport } from '../../../utils/dom/styleChecker.mjs';
import { raf } from '../../../utils/vue/raf.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { findDOMNode } from '../../../utils/vue/props.mjs';
import { useMergedState } from '../../../hooks/use-merged-state/index.mjs';
import { TransButton } from '../../../vue-components/transition/src/transButton.mjs';
import { omit } from '../../../utils/lang/omit.mjs';
import ResizeObserver from '../../../vue-components/vc-resize-observer/src/index.mjs';

const isLineClampSupport = isStyleSupport("webkitLineClamp");
const isTextOverflowSupport = isStyleSupport("textOverflow");
const ELLIPSIS_STR = "...";
var ATypographyBase = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "TypographyBase",
  inheritAttrs: false,
  props: baseProps(),
  // emits: ['update:content'],
  setup(props, {
    slots,
    attrs,
    emit
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject("typography", props);
    const state = reactive({
      copied: false,
      ellipsisText: "",
      ellipsisContent: null,
      isEllipsis: false,
      expanded: false,
      clientRendered: false,
      // locale
      expandStr: "",
      copyStr: "",
      copiedStr: "",
      editStr: "",
      copyId: void 0,
      rafId: void 0,
      prevProps: void 0,
      originContent: ""
    });
    const contentRef = ref();
    const editIcon = ref();
    const ellipsis = computed(() => {
      const ellipsis2 = props.ellipsis;
      if (!ellipsis2)
        return {};
      return {
        rows: 1,
        expandable: false,
        ...typeof ellipsis2 === "object" ? ellipsis2 : null
      };
    });
    onMounted(() => {
      state.clientRendered = true;
      syncEllipsis();
    });
    onBeforeUnmount(() => {
      clearTimeout(state.copyId);
      raf.cancel(state.rafId);
    });
    watch([() => ellipsis.value.rows, () => props.content], () => {
      nextTick(() => {
        resizeOnNextFrame();
      });
    }, {
      flush: "post",
      deep: true
    });
    watchEffect(() => {
      if (props.content === void 0) {
        devWarning(!props.editable, "Typography", "When `editable` is enabled, please use `content` instead of children");
        devWarning(!props.ellipsis, "Typography", "When `ellipsis` is enabled, please use `content` instead of children");
      }
    });
    function getChildrenText() {
      var _a;
      return props.ellipsis || props.editable ? props.content : (_a = findDOMNode(contentRef.value)) == null ? void 0 : _a.innerText;
    }
    function onExpandClick(e) {
      const {
        onExpand
      } = ellipsis.value;
      state.expanded = true;
      onExpand == null ? void 0 : onExpand(e);
    }
    const editable = computed(() => {
      const editable2 = props.editable;
      if (!editable2)
        return {
          editing: false
        };
      return {
        ...typeof editable2 === "object" ? editable2 : null
      };
    });
    function onEditClick(e) {
      e.preventDefault();
      state.originContent = props.content;
      triggerEdit(true);
    }
    function onEditChange(value) {
      onContentChange(value);
      triggerEdit(false);
    }
    function onContentChange(value) {
      const {
        onChange
      } = editable.value;
      if (value !== props.content) {
        emit("update:content", value);
        onChange == null ? void 0 : onChange(value);
      }
    }
    function onEditCancel() {
      var _a, _b;
      (_b = (_a = editable.value).onCancel) == null ? void 0 : _b.call(_a);
      triggerEdit(false);
    }
    function onCopyClick(e) {
      e.preventDefault();
      e.stopPropagation();
      const {
        copyable
      } = props;
      const copyConfig = {
        ...typeof copyable === "object" ? copyable : null
      };
      if (copyConfig.text === void 0)
        copyConfig.text = getChildrenText();
      copy(copyConfig.text || "");
      state.copied = true;
      nextTick(() => {
        if (copyConfig.onCopy)
          copyConfig.onCopy(e);
        state.copyId = setTimeout(() => {
          state.copied = false;
        }, 3e3);
      });
    }
    const [editing, setEditing] = useMergedState(false, {
      value: computed(() => {
        return editable.value.editing;
      })
    });
    function triggerEdit(edit) {
      const {
        onStart
      } = editable.value;
      if (edit && onStart)
        onStart();
      setEditing(edit);
    }
    watch(editing, (val) => {
      var _a;
      if (!val)
        (_a = editIcon.value) == null ? void 0 : _a.focus();
    }, {
      flush: "post"
    });
    const canUseCSSEllipsis = computed(() => {
      const {
        rows,
        expandable,
        suffix,
        onEllipsis,
        tooltip
      } = ellipsis.value;
      if (suffix || tooltip)
        return false;
      if (props.editable || props.copyable || expandable || onEllipsis)
        return false;
      if (rows === 1)
        return isTextOverflowSupport;
      return isLineClampSupport;
    });
    const syncEllipsis = () => {
      const {
        ellipsisText,
        isEllipsis
      } = state;
      const {
        rows,
        suffix,
        onEllipsis
      } = ellipsis.value;
      if (!rows || rows < 0 || !findDOMNode(contentRef.value) || state.expanded || props.content === void 0)
        return;
      if (canUseCSSEllipsis.value)
        return;
      const {
        content,
        text,
        ellipsis: ell
      } = measure(findDOMNode(contentRef.value), {
        rows,
        suffix
      }, props.content, renderOperations(true), ELLIPSIS_STR);
      if (ellipsisText !== text || state.isEllipsis !== ell) {
        state.ellipsisText = text;
        state.ellipsisContent = content;
        state.isEllipsis = ell;
        if (isEllipsis !== ell && onEllipsis)
          onEllipsis(ell);
      }
    };
    function resizeOnNextFrame(sizeInfo) {
      if (sizeInfo) {
        const {
          width,
          height
        } = sizeInfo;
        if (!width || !height)
          return;
      }
      raf.cancel(state.rafId);
      state.rafId = raf(() => {
        syncEllipsis();
      });
    }
    function wrapperDecorations({
      mark,
      code,
      underline,
      delete: del,
      strong,
      keyboard
    }, content) {
      let currentContent = content;
      function wrap(needed, Tag) {
        if (!needed)
          return;
        const _currentContent = function() {
          return currentContent;
        }();
        currentContent = createVNode(Tag, null, {
          default: () => [_currentContent]
        });
      }
      wrap(strong, "strong");
      wrap(underline, "u");
      wrap(del, "del");
      wrap(code, "code");
      wrap(mark, "mark");
      wrap(keyboard, "kbd");
      return currentContent;
    }
    function renderExpand(forceRender) {
      const {
        expandable,
        symbol
      } = ellipsis.value;
      if (!expandable)
        return null;
      if (!forceRender && (state.expanded || !state.isEllipsis))
        return null;
      const expandContent = (slots.ellipsisSymbol ? slots.ellipsisSymbol() : symbol) || state.expandStr;
      return createVNode("a", {
        "key": "expand",
        "class": `${prefixCls.value}-expand`,
        "onClick": onExpandClick,
        "aria-label": state.expandStr
      }, [expandContent]);
    }
    function renderEdit() {
      if (!props.editable)
        return;
      const {
        tooltip,
        triggerType = ["icon"]
      } = props.editable;
      const icon = slots.editableIcon ? slots.editableIcon() : createVNode(EditOutlined, {
        "role": "button"
      }, null);
      const title = slots.editableTooltip ? slots.editableTooltip() : state.editStr;
      const ariaLabel = typeof title === "string" ? title : "";
      return triggerType.includes("icon") ? createVNode(Tooltip, {
        "key": "edit",
        "title": tooltip === false ? "" : title
      }, {
        default: () => [createVNode(TransButton, {
          "ref": editIcon,
          "class": `${prefixCls.value}-edit`,
          "onClick": onEditClick,
          "aria-label": ariaLabel
        }, {
          default: () => [icon]
        })]
      }) : null;
    }
    function renderCopy() {
      if (!props.copyable)
        return;
      const {
        tooltip
      } = props.copyable;
      const defaultTitle = state.copied ? state.copiedStr : state.copyStr;
      const title = slots.copyableTooltip ? slots.copyableTooltip({
        copied: state.copied
      }) : defaultTitle;
      const ariaLabel = typeof title === "string" ? title : "";
      const defaultIcon = state.copied ? createVNode(CheckOutlined, null, null) : createVNode(CopyOutlined, null, null);
      const icon = slots.copyableIcon ? slots.copyableIcon({
        copied: !!state.copied
      }) : defaultIcon;
      return createVNode(Tooltip, {
        "key": "copy",
        "title": tooltip === false ? "" : title
      }, {
        default: () => [createVNode(TransButton, {
          "class": [`${prefixCls.value}-copy`, {
            [`${prefixCls.value}-copy-success`]: state.copied
          }],
          "onClick": onCopyClick,
          "aria-label": ariaLabel
        }, {
          default: () => [icon]
        })]
      });
    }
    function renderEditInput() {
      const {
        class: className,
        style
      } = attrs;
      const {
        maxlength,
        autoSize,
        onEnd
      } = editable.value;
      return createVNode(Editable, {
        "class": className,
        "style": style,
        "prefixCls": prefixCls.value,
        "value": props.content,
        "originContent": state.originContent,
        "maxlength": maxlength,
        "autoSize": autoSize,
        "onSave": onEditChange,
        "onChange": onContentChange,
        "onCancel": onEditCancel,
        "onEnd": onEnd,
        "direction": direction.value,
        "component": props.component
      }, {
        enterIcon: slots.editableEnterIcon
      });
    }
    function renderOperations(forceRenderExpanded) {
      return [renderExpand(forceRenderExpanded), renderEdit(), renderCopy()].filter((node) => node);
    }
    return () => {
      var _a;
      const {
        triggerType = ["icon"]
      } = editable.value;
      const children = props.ellipsis || props.editable ? props.content !== void 0 ? props.content : (_a = slots.default) == null ? void 0 : _a.call(slots) : slots.default ? slots.default() : props.content;
      if (editing.value)
        return renderEditInput();
      return createVNode(LocaleReceiver, {
        "componentName": "Text",
        "children": (locale) => {
          const {
            type,
            disabled,
            content,
            class: className,
            style,
            ...restProps
          } = {
            ...props,
            ...attrs
          };
          const {
            rows,
            suffix,
            tooltip
          } = ellipsis.value;
          const {
            edit,
            copy: copyStr,
            copied,
            expand
          } = locale;
          state.editStr = edit;
          state.copyStr = copyStr;
          state.copiedStr = copied;
          state.expandStr = expand;
          const textProps = omit(restProps, ["prefixCls", "editable", "copyable", "ellipsis", "mark", "code", "delete", "underline", "strong", "keyboard", "onUpdate:content"]);
          const cssEllipsis = canUseCSSEllipsis.value;
          const cssTextOverflow = rows === 1 && cssEllipsis;
          const cssLineClamp = rows && rows > 1 && cssEllipsis;
          let textNode = children;
          let ariaLabel;
          if (rows && state.isEllipsis && !state.expanded && !cssEllipsis) {
            const {
              title: title2
            } = restProps;
            let restContent = title2 || "";
            if (!title2 && (typeof children === "string" || typeof children === "number"))
              restContent = String(children);
            restContent = restContent == null ? void 0 : restContent.slice(String(state.ellipsisContent || "").length);
            textNode = createVNode(Fragment, null, [toRaw(state.ellipsisContent), createVNode("span", {
              "title": restContent,
              "aria-hidden": "true"
            }, [ELLIPSIS_STR]), suffix]);
          } else {
            textNode = createVNode(Fragment, null, [children, suffix]);
          }
          textNode = wrapperDecorations(props, textNode);
          const showTooltip = tooltip && rows && state.isEllipsis && !state.expanded && !cssEllipsis;
          const title = slots.ellipsisTooltip ? slots.ellipsisTooltip() : tooltip;
          return createVNode(ResizeObserver, {
            "onResize": resizeOnNextFrame,
            "disabled": !rows
          }, {
            default: () => [createVNode(Typography, {
              "ref": contentRef,
              "class": [{
                [`${prefixCls.value}-${type}`]: type,
                [`${prefixCls.value}-disabled`]: disabled,
                [`${prefixCls.value}-ellipsis`]: rows,
                [`${prefixCls.value}-single-line`]: rows === 1 && !state.isEllipsis,
                [`${prefixCls.value}-ellipsis-single-line`]: cssTextOverflow,
                [`${prefixCls.value}-ellipsis-multiple-line`]: cssLineClamp
              }, className],
              "style": {
                ...style,
                WebkitLineClamp: cssLineClamp ? rows : void 0
              },
              "aria-label": ariaLabel,
              "direction": direction.value,
              "onClick": triggerType.includes("text") ? onEditClick : () => {
              },
              ...textProps
            }, {
              default: () => [showTooltip ? createVNode(Tooltip, {
                "title": tooltip === true ? children : title
              }, {
                default: () => [createVNode("span", null, [textNode])]
              }) : textNode, renderOperations()]
            })]
          });
        }
      }, null);
    };
  }
});

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