import { defineComponent as N, inject as G, ref as y, computed as x, watch as p, nextTick as f, unref as r, onMounted as U, createElementBlock as _, openBlock as b, createElementVNode as u, withDirectives as J, createVNode as w, renderSlot as T, toDisplayString as Q, withKeys as X, vModelText as Z, normalizeClass as ee } from "vue";
import { useImmediateDebouncedRef as te } from "../../../hooks/useImmediateDebouncedRef.js";
import { useMessageDraft as ne } from "../../../hooks/useMessageDraft.js";
import "../../../hooks/useSearchModel.js";
import "../../../hooks/useTheme.js";
/* empty css                              */
/* empty css                                              */
import "../../../node_modules/linkifyjs/dist/linkify.js";
import "../../../node_modules/linkify-string/dist/linkify-string.js";
import "../../../functions/parseMarkdown.js";
import { t as c } from "../../../locale/useLocale.js";
import le from "./icons/SendIcon.vue.js";
import ie from "../../2_chatinput_elements/TextFormatToolbar/TextFormatToolbar.vue.js";
const oe = { class: "chat-input__container" }, se = ["id"], ae = ["id"], re = { class: "chat-input__inline-buttons" }, ue = {
  key: 0,
  class: "chat-input__input chat-input__disabled-placeholder"
}, ce = ["disabled", "placeholder"], de = ["disabled"], pe = { class: "" }, fe = { class: "chat-input__third-line" }, De = /* @__PURE__ */ N({
  __name: "ChatInput",
  props: {
    state: {
      type: String,
      required: !1,
      default: "active"
    },
    focusOnInputArea: {
      type: Boolean,
      required: !1,
      default: !1
    },
    disabledPlaceholder: {
      type: String,
      default: null
    },
    selectedChannel: {
      type: Object,
      required: !1,
      default: null
    },
    inputButtonColor: {
      type: String,
      required: !1,
      default: null
    }
  },
  emits: ["send", "typing"],
  setup(d, { emit: A }) {
    const h = A, m = G("chatAppId"), { resetMessage: D, getMessage: i, setMessageText: v, setForceSendMessage: F } = ne(m), n = y(), E = te("", 2e3), M = y(), a = d, P = x(() => !!(a.state == "disabled" || i().text == "" && !i().file || i().isRecording)), R = x(() => {
      var t;
      if (a.inputButtonColor)
        return a.inputButtonColor;
      if (!((t = a.selectedChannel) != null && t.channelId))
        return "#25D366";
      const e = a.selectedChannel.channelId.toLowerCase();
      return e.includes("whatsapp") || e.includes("waba") ? "#25D366" : e.includes("telegram") ? "#37AFE2" : e.includes("sms") ? "#6C757D" : e.includes("max") ? "#4B0082" : "#25D366";
    }), k = x(() => {
      var t;
      if (!((t = a.selectedChannel) != null && t.channelId))
        return c("component.ChatInput.InputPlaceholder");
      const e = a.selectedChannel.channelId.toLowerCase();
      return e.includes("whatsapp") || e.includes("waba") ? c("component.ChatInput.WhatsappInputPlaceholder") : e.includes("telegram") ? c("component.ChatInput.TelegramInputPlaceholder") : e.includes("sms") ? c("component.ChatInput.SmsInputPlaceholder") : e.includes("max") ? c("component.ChatInput.MaxInputPlaceholder") : c("component.ChatInput.InputPlaceholder");
    });
    p(
      () => E.value,
      () => {
        h("typing");
      }
    ), p(
      () => a.focusOnInputArea,
      () => {
        a.focusOnInputArea && f(() => {
          const e = r(n);
          e == null || e.focus();
        });
      },
      { immediate: !0 }
    ), p(
      () => i().text,
      () => {
        f(() => {
          const e = n.value;
          if (!e) return;
          const t = e.scrollTop;
          e.style.height = "auto";
          const l = getComputedStyle(e), o = parseFloat(l.fontSize) || 16, W = parseFloat(l.lineHeight) || o * 1.4, I = 40, C = W * 11, S = e.scrollHeight, O = i().text.split(`
`).length > 1, s = document.createElement("div");
          s.style.position = "absolute", s.style.visibility = "hidden", s.style.whiteSpace = "nowrap", s.style.font = l.font, s.style.fontSize = l.fontSize, s.style.fontFamily = l.fontFamily, s.style.fontWeight = l.fontWeight, s.style.letterSpacing = l.letterSpacing, s.textContent = i().text, document.body.appendChild(s);
          const Y = s.offsetWidth;
          document.body.removeChild(s);
          const q = parseFloat(l.paddingLeft) || 0, K = parseFloat(l.paddingRight) || 0, j = e.clientWidth - q - K, V = Y > j, $ = O || V;
          i().text.trim() && $ ? S <= C ? (e.style.height = S + "px", e.style.overflowY = "hidden") : (e.style.height = C + "px", e.style.overflowY = "auto", e.scrollTop = t) : (e.style.height = I + "px", e.style.overflowY = "hidden");
        });
      },
      { immediate: !0 }
    ), p(
      () => i().forceSend,
      () => {
        i().forceSend && (g(), F(!1));
      }
    );
    const z = (e) => {
      const t = e.target;
      h("typing", t.value);
    }, B = () => {
      f(() => {
        const e = n.value;
        e && (e.style.height = "40px", e.style.overflowY = "hidden");
      });
    };
    U(() => {
      B();
    });
    const H = (e) => {
      if (e.shiftKey) {
        if (n.value instanceof HTMLTextAreaElement) {
          let t = n.value.selectionStart;
          t && (n.value.setRangeText(`
`, t, t, "end"), v(n.value.value));
        }
        e.preventDefault();
      } else if (e.ctrlKey) {
        if (n.value instanceof HTMLTextAreaElement) {
          let t = n.value.selectionStart;
          t && (n.value.setRangeText(`
`, t, t, "end"), v(n.value.value));
        }
        e.preventDefault();
      } else
        e.preventDefault(), g();
    }, L = (e) => {
      if (n.value) {
        const t = n.value.value;
        n.value.value = e.newText;
        const l = e.newText.length - (t.length - (e.end - e.start)), o = e.start + l;
        v(e.newText), f(() => {
          n.value && (n.value.setSelectionRange(o, o), n.value.focus());
        });
      }
    }, g = () => {
      var t, l;
      const e = y(i());
      if (e.value.text != "" || e.value.file) {
        const o = {
          type: "",
          text: "",
          url: "",
          filename: "",
          size: "",
          reply: void 0
        };
        e.value.file ? (o.type = "message." + e.value.file.type, o.url = e.value.file.url, o.filename = e.value.file.name, o.size = (t = e.value.file.size) == null ? void 0 : t.toString(), o.text = (l = e == null ? void 0 : e.value) == null ? void 0 : l.text.trim()) : (o.type = "message.text", o.text = e.value.text.trim()), e.value.reply && (o.reply = e.value.reply), h("send", o), D(), M.value = void 0, n.value && n.value.focus();
      }
    };
    return (e, t) => (b(), _("div", oe, [
      u("div", {
        id: "chat-input-reply-line-" + r(m),
        class: "chat-input__reply-line"
      }, null, 8, se),
      u("div", {
        id: "chat-input-file-line-" + r(m),
        class: "chat-input__file-line"
      }, null, 8, ae),
      u("div", re, [
        T(e.$slots, "inline-buttons", {}, void 0, !0)
      ]),
      d.disabledPlaceholder && (d.state == "disabled" || r(i)().isRecording) ? (b(), _("div", ue, Q(d.disabledPlaceholder), 1)) : J((b(), _("textarea", {
        key: 1,
        ref_key: "refInput",
        ref: n,
        "onUpdate:modelValue": t[0] || (t[0] = (l) => r(i)().text = l),
        disabled: d.state == "disabled" || r(i)().isRecording,
        class: "chat-input__input",
        placeholder: k.value,
        onKeydown: X(H, ["enter"]),
        onInput: z
      }, null, 40, ce)), [
        [Z, r(i)().text]
      ]),
      w(ie, {
        textarea: n.value,
        onFormatApplied: L
      }, null, 8, ["textarea"]),
      u("button", {
        class: ee(["chat-input__button", { "chat-input__button-disabled": P.value }]),
        disabled: r(i)().isRecording,
        onClick: g
      }, [
        u("span", pe, [
          w(r(le), { color: R.value }, null, 8, ["color"])
        ])
      ], 10, de),
      u("div", fe, [
        T(e.$slots, "buttons", {}, void 0, !0)
      ])
    ]));
  }
});
export {
  De as default
};
