import { ref as h, computed as b, watch as P, createElementBlock as s, openBlock as a, createElementVNode as o, Fragment as O, renderList as R, normalizeClass as S, withDirectives as z, vModelRadio as F, createTextVNode as j, createCommentVNode as C, createBlock as g, toDisplayString as k, resolveDynamicComponent as x, createVNode as U } from "vue";
import W from "../../1_icons/ContactCRMIcon.vue.js";
import G from "../../1_icons/CheckIcon.vue.js";
import H from "../../1_icons/WhatsAppIcon.vue.js";
import J from "../../1_icons/TelegramIcon.vue.js";
/* empty css                 */
import K from "../../../_virtual/_plugin-vue_export-helper.js";
const Q = { class: "contact-info" }, X = { class: "contact-info__attribute-list" }, Y = ["id", "value", "onChange"], Z = ["for"], tt = { class: "contact-info__custom-radio" }, et = { class: "contact-info__radio-inner" }, nt = {
  key: 0,
  class: "contact-info__badge"
}, ot = { class: "contact-info__channel-section" }, ct = { class: "contact-info__custom-select" }, at = { class: "contact-info__icon" }, lt = ["src", "alt"], it = {
  key: 0,
  class: "contact-info__select-options"
}, st = ["onClick"], rt = { class: "contact-info__icon" }, ut = ["src", "alt"], dt = { class: "contact-info__crm-button" }, _t = {
  __name: "ContactInfo",
  props: {
    contact: {
      type: Object,
      required: !1,
      default: () => ({ attributes: [] })
    },
    currentDialog: {
      type: Object,
      default: () => ({})
    },
    channels: {
      type: Array,
      required: !0
    }
  },
  emits: ["close", "open-crm", "select-attribute-channel"],
  setup(y, { emit: B }) {
    var V, A, N;
    const c = y, I = B, l = h(((N = (A = (V = c.contact) == null ? void 0 : V.attributes) == null ? void 0 : A[0]) == null ? void 0 : N.id) || ""), f = h(c.channels[0] || null), u = h(!1), L = (t) => {
      f.value = t, u.value = !1, console.log(`Выбран канал связи: ${t.title}`), I("select-attribute-channel", {
        attributeId: l.value,
        channelId: t.channelId
      });
    }, M = (t) => (t == null ? void 0 : t.replace(/\D/g, "")) || "", w = b(() => {
      var t, e;
      return (e = (t = c.contact) == null ? void 0 : t.attributes) == null ? void 0 : e.find((i) => i.id === l.value);
    }), d = b(() => {
      if (f.value)
        return f.value;
      if (!w.value) return c.channels[0] || null;
      const t = M(w.value.value);
      return t ? c.channels.find((i) => {
        var r, _;
        const v = ((r = i.title) == null ? void 0 : r.toLowerCase()) || "", m = ((_ = i.channelId) == null ? void 0 : _.toLowerCase()) || "", p = v.includes(t), n = ["whatsapp", "waba", "telegram"].some((E) => m.includes(E));
        return p && n;
      }) || c.channels[0] || { title: "default", channelId: "fallback" } : c.channels[0] || null;
    }), T = b(() => c.channels), $ = (t) => t ? t.includes("whatsapp") || t.includes("waba") ? H : t.includes("telegram") ? J : null : null, D = (t) => !c.currentDialog || !Object.keys(c.currentDialog) ? !1 : c.currentDialog.attributeId === t.id, q = (t) => {
      I("select-attribute-channel", {
        attributeId: t.id,
        channelId: f.value.channelId
      });
    };
    return P(
      () => c.contact,
      () => {
        var t, e;
        (e = (t = c.contact) == null ? void 0 : t.attributes) != null && e.length ? l.value && c.contact.attributes.some((i) => i.id === l.value) || (l.value = c.contact.attributes[0].id) : l.value = "";
      },
      { immediate: !0 }
    ), (t, e) => {
      var i, v, m, p;
      return a(), s("div", Q, [
        o("div", X, [
          (a(!0), s(O, null, R(((i = y.contact) == null ? void 0 : i.attributes) || [], (n, r) => (a(), s("div", {
            key: n.id,
            class: S(["contact-info__attribute-item", { "contact-info__attribute-item--current-chat": D(n) }])
          }, [
            z(o("input", {
              id: `attr-${r}`,
              "onUpdate:modelValue": e[0] || (e[0] = (_) => l.value = _),
              type: "radio",
              value: n.id,
              onChange: (_) => q(n)
            }, null, 40, Y), [
              [F, l.value]
            ]),
            o("label", {
              for: `attr-${r}`
            }, [
              o("span", tt, [
                o("span", et, [
                  l.value === n.id ? (a(), g(G, { key: 0 })) : C("", !0)
                ])
              ]),
              j(" " + k(n.value) + " ", 1),
              D(n) ? (a(), s("span", nt, " Текущий чат ")) : C("", !0)
            ], 8, Z)
          ], 2))), 128))
        ]),
        o("div", ot, [
          e[3] || (e[3] = o("label", { class: "contact-info__channel-label" }, "Канал связи", -1)),
          o("div", ct, [
            o("div", {
              class: "contact-info__select-trigger",
              onClick: e[1] || (e[1] = (n) => u.value = !u.value)
            }, [
              o("div", at, [
                (v = d.value) != null && v.icon ? (a(), s("img", {
                  key: 0,
                  src: d.value.icon,
                  alt: d.value.title,
                  width: "22",
                  height: "22"
                }, null, 8, lt)) : (a(), g(x($((m = d.value) == null ? void 0 : m.channelId)), {
                  key: 1,
                  class: "contact-info__icon-component"
                }))
              ]),
              o("span", null, k((p = d.value) == null ? void 0 : p.title), 1),
              o("div", {
                class: S(["contact-info__arrow", { "contact-info__arrow--open": u.value }])
              }, null, 2)
            ]),
            u.value ? (a(), s("div", it, [
              (a(!0), s(O, null, R(T.value, (n) => (a(), s("div", {
                key: n.channelId,
                class: "contact-info__select-option",
                onClick: (r) => L(n)
              }, [
                o("div", rt, [
                  n.icon ? (a(), s("img", {
                    key: 0,
                    src: n.icon,
                    alt: n.title,
                    width: "22",
                    height: "22"
                  }, null, 8, ut)) : (a(), g(x($(n.channelId)), {
                    key: 1,
                    class: "contact-info__icon-component"
                  }))
                ]),
                o("span", null, k(n.title), 1)
              ], 8, st))), 128))
            ])) : C("", !0)
          ])
        ]),
        e[5] || (e[5] = o("div", { class: "contact-info__divider" }, null, -1)),
        o("div", dt, [
          o("button", {
            class: "contact-info__button",
            onClick: e[2] || (e[2] = (n) => t.$emit("open-crm"))
          }, [
            U(W),
            e[4] || (e[4] = j(" Открыть контакт в CRM ", -1))
          ])
        ])
      ]);
    };
  }
}, gt = /* @__PURE__ */ K(_t, [["__scopeId", "data-v-42660d89"]]);
export {
  gt as default
};
