<script>
import theme from "#build/b24ui/dashboard-search";
</script>

<script setup>
import { computed, useTemplateRef } from "vue";
import { useForwardProps } from "reka-ui";
import { defu } from "defu";
import { reactivePick } from "@vueuse/core";
import { useAppConfig, useColorMode, defineShortcuts, useRuntimeHook } from "#imports";
import { useLocale } from "../composables/useLocale";
import { omit, transformUI } from "../utils";
import { tv } from "../utils/tv";
import icons from "../dictionary/icons";
import B24CommandPalette from "./CommandPalette.vue";
import B24Modal from "./Modal.vue";
const props = defineProps({
  icon: { type: [Function, Object], required: false },
  placeholder: { type: String, required: false },
  autofocus: { type: Boolean, required: false },
  loading: { type: Boolean, required: false },
  close: { type: [Boolean, Object], required: false, default: true },
  closeIcon: { type: [Function, Object], required: false },
  shortcut: { type: String, required: false, default: "meta_k" },
  groups: { type: Array, required: false },
  fuse: { type: Object, required: false },
  colorMode: { type: Boolean, required: false, default: true },
  class: { type: null, required: false },
  b24ui: { type: void 0, required: false },
  title: { type: String, required: false },
  description: { type: String, required: false },
  overlay: { type: Boolean, required: false },
  transition: { type: Boolean, required: false },
  content: { type: Object, required: false },
  dismissible: { type: Boolean, required: false },
  fullscreen: { type: Boolean, required: false, default: false },
  modal: { type: Boolean, required: false },
  portal: { type: [Boolean, String], required: false, skipCheck: true }
});
const slots = defineSlots();
const open = defineModel("open", { type: Boolean, ...{ default: false } });
const searchTerm = defineModel("searchTerm", { type: String, ...{ default: "" } });
useRuntimeHook("dashboard:search:toggle", () => {
  open.value = !open.value;
});
const { t } = useLocale();
const colorMode = useColorMode();
const appConfig = useAppConfig();
const commandPaletteProps = useForwardProps(reactivePick(props, "icon", "placeholder", "autofocus", "loading", "close", "closeIcon"));
const modalProps = useForwardProps(reactivePick(props, "overlay", "transition", "content", "dismissible", "fullscreen", "modal", "portal"));
const getProxySlots = () => omit(slots, ["content"]);
const fuse = computed(() => defu({}, props.fuse, {
  fuseOptions: {}
}));
const b24ui = computed(() => tv({ extend: tv(theme), ...appConfig.b24ui?.dashboardSearch || {} })({
  fullscreen: props.fullscreen
}));
const groups = computed(() => {
  const groups2 = [];
  groups2.push(...props.groups || []);
  if (props.colorMode && !colorMode?.forced) {
    groups2.push({
      id: "theme",
      label: t("dashboardSearch.theme"),
      items: [
        {
          label: t("colorMode.system"),
          icon: icons.system,
          active: colorMode.preference === "system",
          onSelect: () => {
            colorMode.preference = "system";
          }
        },
        {
          label: t("colorMode.light"),
          icon: icons.light,
          active: colorMode.preference === "light",
          onSelect: () => {
            colorMode.preference = "light";
          }
        },
        {
          label: t("colorMode.dark"),
          icon: icons.dark,
          active: colorMode.preference === "dark",
          onSelect: () => {
            colorMode.preference = "dark";
          }
        }
      ]
    });
  }
  return groups2;
});
const commandPaletteRef = useTemplateRef("commandPaletteRef");
function onSelect(item) {
  if (item.disabled) {
    return;
  }
  open.value = false;
  searchTerm.value = "";
}
defineShortcuts({
  [props.shortcut]: {
    usingInput: true,
    handler: () => open.value = !open.value
  }
});
defineExpose({
  commandPaletteRef
});
</script>

<template>
  <B24Modal
    v-model:open="open"
    :title="title || t('dashboardSearch.title')"
    :description="description || t('dashboardSearch.description')"
    v-bind="modalProps"
    data-slot="modal"
    :class="b24ui.modal({ class: [props.b24ui?.modal, props.class] })"
  >
    <template #content="contentData">
      <slot name="content" v-bind="contentData">
        <B24CommandPalette
          ref="commandPaletteRef"
          v-model:search-term="searchTerm"
          v-bind="commandPaletteProps"
          :groups="groups"
          :fuse="fuse"
          :b24ui="transformUI(omit(b24ui, ['modal']), props.b24ui)"
          @update:model-value="onSelect"
          @update:open="open = $event"
        >
          <template v-for="(_, name) in getProxySlots()" #[name]="slotData">
            <slot :name="name" v-bind="slotData" />
          </template>
        </B24CommandPalette>
      </slot>
    </template>
  </B24Modal>
</template>
