"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const icon = require("../../../../@vxe-ui/core/es/src/icon.js");
const index_esm = require("../../../../@vxe-ui/core/es/index.esm.js");
const i18n = require("../../../../@vxe-ui/core/es/src/i18n.js");
const VxeTextComponent = comp.defineVxeComponent({
  name: "VxeText",
  props: {
    status: String,
    title: [String, Number],
    icon: String,
    prefixIcon: String,
    suffixIcon: String,
    loading: Boolean,
    content: [String, Number],
    clickToCopy: {
      type: Boolean,
      default: () => config.getConfig().text.clickToCopy
    },
    copyConfig: Object,
    size: {
      type: String,
      default: () => config.getConfig().text.size || config.getConfig().size
    }
  },
  emits: [
    "click",
    "dblclick",
    "prefix-click",
    "suffix-click",
    "copy-success",
    "copy-error"
  ],
  setup(props, context) {
    const { emit, slots } = context;
    const xID = XEUtils.uniqueId();
    const { computeSize } = useFns.useSize(props);
    const refElem = vue.ref();
    const refContentElem = vue.ref();
    const reactData = vue.reactive({});
    const computeCopyOpts = vue.computed(() => {
      return Object.assign({}, config.getConfig().text.copyConfig, props.copyConfig);
    });
    const refMaps = {
      refElem
    };
    const computeMaps = {};
    const handleCopy = (evnt) => {
      const { content } = props;
      const copyOpts = computeCopyOpts.value;
      const { showMessage } = copyOpts;
      const contentEl = refContentElem.value;
      const copyVal = (contentEl ? contentEl.textContent : "") || content;
      if (copyVal) {
        if (index_esm.VxeUI.clipboard.copy(copyVal)) {
          if (showMessage && index_esm.VxeUI.modal) {
            index_esm.VxeUI.modal.message({
              content: i18n.getI18n("vxe.text.copySuccess"),
              status: "success"
            });
          }
          dispatchEvent("copy-success", {}, evnt);
        } else {
          if (showMessage && index_esm.VxeUI.modal) {
            index_esm.VxeUI.modal.message({
              content: i18n.getI18n("vxe.text.copyError"),
              status: "error"
            });
          }
          dispatchEvent("copy-error", {}, evnt);
        }
      }
    };
    const clickIconEvent = (evnt) => {
      const { clickToCopy } = props;
      const copyOpts = computeCopyOpts.value;
      if (clickToCopy && copyOpts.trigger !== "dblclick") {
        handleCopy(evnt);
      }
    };
    const dblclickIconEvent = (evnt) => {
      const { clickToCopy } = props;
      const copyOpts = computeCopyOpts.value;
      if (clickToCopy && copyOpts.trigger === "dblclick") {
        handleCopy(evnt);
      }
    };
    const $xeText = {
      xID,
      props,
      context,
      reactData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $text: $xeText }, params));
    };
    const textMethods = {
      dispatchEvent
    };
    const clickEvent = (evnt) => {
      const { loading } = props;
      if (!loading) {
        dispatchEvent("click", {}, evnt);
      }
    };
    const dblclickEvent = (evnt) => {
      const { loading } = props;
      if (!loading) {
        dispatchEvent("dblclick", {}, evnt);
      }
    };
    const prefixEvent = (evnt) => {
      const { loading } = props;
      if (!loading) {
        dispatchEvent("prefix-click", {}, evnt);
      }
    };
    const suffixEvent = (evnt) => {
      const { loading } = props;
      if (!loading) {
        dispatchEvent("suffix-click", {}, evnt);
      }
    };
    const textPrivateMethods = {};
    Object.assign($xeText, textMethods, textPrivateMethods);
    const renderCopyIcon = () => {
      const copyOpts = computeCopyOpts.value;
      const { icon: icon$1, status } = copyOpts;
      return vue.h("span", {
        key: "ci",
        class: ["vxe-text--copy-icon", {
          [`theme--${status}`]: status
        }],
        onClick: clickIconEvent,
        onDblclick: dblclickIconEvent
      }, [
        vue.h("i", {
          class: icon$1 || icon.getIcon().TEXT_COPY
        })
      ]);
    };
    const renderContent = () => {
      const { loading, icon: icon$1, prefixIcon, suffixIcon, clickToCopy, content } = props;
      const copyOpts = computeCopyOpts.value;
      const defaultSlot = slots.default;
      const prefixIconSlot = slots.prefixIcon || slots["prefix-icon"] || slots.icon;
      const suffixIconSlot = slots.suffixIcon || slots["suffix-icon"];
      const copyToRight = copyOpts.layout === "right";
      const contVNs = [];
      if (loading) {
        contVNs.push(vue.h("span", {
          key: "lg",
          class: "vxe-text--loading"
        }, [
          vue.h("i", {
            class: icon.getIcon().TEXT_LOADING
          })
        ]));
      } else if (clickToCopy && !copyToRight) {
        contVNs.push(renderCopyIcon());
      }
      if (prefixIcon || icon$1) {
        contVNs.push(vue.h("span", {
          key: "si",
          class: "vxe-text--prefix-icon",
          onClick: prefixEvent
        }, prefixIconSlot ? prefixIconSlot({}) : [
          vue.h("i", {
            class: prefixIcon || icon$1
          })
        ]));
      }
      contVNs.push(vue.h("span", {
        key: "ct",
        ref: refContentElem,
        class: "vxe-text--content",
        onClick: clickEvent,
        onDblclick: dblclickEvent
      }, defaultSlot ? defaultSlot({}) : XEUtils.toValueString(content)));
      if (suffixIcon) {
        contVNs.push(vue.h("span", {
          key: "si",
          class: "vxe-text--suffix-icon",
          onClick: suffixEvent
        }, suffixIconSlot ? suffixIconSlot({}) : [
          vue.h("i", {
            class: suffixIcon
          })
        ]));
      }
      if (clickToCopy && copyToRight && !loading) {
        contVNs.push(renderCopyIcon());
      }
      return contVNs;
    };
    const renderVN = () => {
      const { loading, status, title, clickToCopy } = props;
      const vSize = computeSize.value;
      return vue.h("span", {
        ref: refElem,
        title,
        class: ["vxe-text", {
          [`size--${vSize}`]: vSize,
          [`theme--${status}`]: status,
          "is--copy": clickToCopy,
          "is--loading": loading
        }]
      }, renderContent());
    };
    $xeText.renderVN = renderVN;
    return $xeText;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeTextComponent;
