import { defineComponent as D, computed as i, ref as x, createBlock as v, openBlock as f, withCtx as o, createVNode as g, createTextVNode as k, toDisplayString as N, createCommentVNode as C, unref as c, createElementVNode as T, normalizeClass as y } from "vue";
import { ScalarListbox as h, ScalarButton as S, ScalarIcon as U } from "@scalar/components";
import j from "./CommandActionForm.vue.js";
import z from "./CommandActionInput.vue.js";
const E = {
  name: "CommandPaletteTag"
}, M = /* @__PURE__ */ D({
  ...E,
  props: {
    workspaceStore: {},
    eventBus: {},
    documentName: {},
    tag: {}
  },
  emits: ["close", "back"],
  setup(e, { emit: p }) {
    const m = p, n = i(() => e.tag !== void 0), s = x(e.tag?.name ?? ""), u = i(() => s.value.trim()), r = i(
      () => Object.entries(e.workspaceStore.workspace.documents).map(
        ([a, l]) => ({
          id: a,
          label: l.info.title || a
        })
      )
    ), t = x(
      e.documentName ? r.value.find((a) => a.id === e.documentName) : r.value[0] ?? void 0
    ), b = i(() => {
      const a = e.workspaceStore.workspace.documents[t.value?.id ?? ""];
      return !!(!u.value || !t.value || !a || n.value && u.value === e.tag?.name || a.tags?.some((l) => l.name === u.value));
    }), w = () => {
      if (!(b.value || !t.value)) {
        if (n.value && e.tag) {
          e.eventBus.emit(
            "tag:edit:tag",
            {
              tag: e.tag,
              documentName: t.value.id,
              newName: u.value
            },
            { skipUnpackProxy: !0 }
          ), m("close");
          return;
        }
        e.eventBus.emit("tag:create:tag", {
          name: u.value,
          documentName: t.value.id
        }), m("close");
      }
    }, V = (a) => {
      n.value || m("back", a);
    }, B = () => {
      m("close");
    };
    return (a, l) => (f(), v(j, {
      disabled: b.value,
      onSubmit: w
    }, {
      options: o(() => [
        n.value ? C("", !0) : (f(), v(c(h), {
          key: 0,
          modelValue: t.value,
          "onUpdate:modelValue": l[1] || (l[1] = (d) => t.value = d),
          options: r.value
        }, {
          default: o(() => [
            g(c(S), {
              class: "hover:bg-b-2 max-h-8 w-fit justify-between gap-1 p-2 text-xs",
              variant: "outlined"
            }, {
              default: o(() => [
                T("span", {
                  class: y(t.value ? "text-c-1" : "text-c-3")
                }, N(t.value ? t.value.label : "Select Collection"), 3),
                g(c(U), {
                  class: "text-c-3",
                  icon: "ChevronDown",
                  size: "md"
                })
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["modelValue", "options"])),
        n.value ? (f(), v(c(S), {
          key: 1,
          class: "max-h-8 px-3 text-xs",
          variant: "outlined",
          onClick: B
        }, {
          default: o(() => [...l[2] || (l[2] = [
            k(" Cancel ", -1)
          ])]),
          _: 1
        })) : C("", !0)
      ]),
      submit: o(() => [
        k(N(n.value ? "Save" : "Create Tag"), 1)
      ]),
      default: o(() => [
        g(z, {
          modelValue: s.value,
          "onUpdate:modelValue": l[0] || (l[0] = (d) => s.value = d),
          label: "Tag Name",
          placeholder: "Tag Name",
          onDelete: V
        }, null, 8, ["modelValue"])
      ]),
      _: 1
    }, 8, ["disabled"]));
  }
});
export {
  M as default
};
