<script>
import theme from "#build/b24ui/switch";
</script>

<script setup>
import { computed, useId } from "vue";
import { Primitive, SwitchRoot, SwitchThumb, useForwardProps, Label } from "reka-ui";
import { reactivePick } from "@vueuse/core";
import { useAppConfig } from "#imports";
import { useFormField } from "../composables/useFormField";
import { tv } from "../utils/tv";
import { omit } from "../utils";
import icons from "../dictionary/icons";
defineOptions({ inheritAttrs: false });
const props = defineProps({
  as: { type: null, required: false },
  color: { type: null, required: false },
  size: { type: null, required: false },
  loading: { type: Boolean, required: false },
  loadingIcon: { type: [Function, Object], required: false },
  checkedIcon: { type: [Function, Object], required: false },
  uncheckedIcon: { type: [Function, Object], required: false },
  label: { type: String, required: false },
  description: { type: String, required: false },
  class: { type: null, required: false },
  b24ui: { type: null, required: false },
  disabled: { type: Boolean, required: false },
  id: { type: String, required: false },
  name: { type: String, required: false },
  required: { type: Boolean, required: false },
  value: { type: String, required: false },
  defaultValue: { type: Boolean, required: false }
});
const slots = defineSlots();
const emits = defineEmits(["change"]);
const modelValue = defineModel({ type: Boolean, ...{ default: void 0 } });
const appConfig = useAppConfig();
const rootProps = useForwardProps(reactivePick(props, "required", "value", "defaultValue"));
const { id: _id, emitFormChange, emitFormInput, size, color, name, disabled, ariaAttrs } = useFormField(props);
const id = _id.value ?? useId();
const b24ui = computed(() => tv({ extend: tv(theme), ...appConfig.b24ui?.switch || {} })({
  size: size.value,
  color: color.value,
  required: props.required,
  loading: props.loading,
  disabled: disabled.value || props.loading
}));
function onUpdate(value) {
  const event = new Event("change", { target: { value } });
  emits("change", event);
  emitFormChange();
  emitFormInput();
}
</script>

<template>
  <Primitive :as="as" data-slot="root" :class="b24ui.root({ class: [props.b24ui?.root, props.class] })">
    <div data-slot="container" :class="b24ui.container({ class: props.b24ui?.container })">
      <SwitchRoot
        :id="id"
        v-bind="{ ...rootProps, ...omit({ ...$attrs }, ['data-state']), ...ariaAttrs }"
        v-model="modelValue"
        :name="name"
        :disabled="disabled || loading"
        data-slot="base"
        :class="b24ui.base({ class: props.b24ui?.base })"
        @update:model-value="onUpdate"
      >
        <SwitchThumb data-slot="thumb" :class="b24ui.thumb({ class: props.b24ui?.thumb })">
          <Component
            :is="loadingIcon || icons.refresh"
            v-if="loading"
            data-slot="icon"
            :class="b24ui.icon({ class: props.b24ui?.icon, checked: true, unchecked: true })"
          />
          <template v-else>
            <Component
              :is="checkedIcon"
              v-if="checkedIcon"
              data-slot="icon"
              :class="b24ui.icon({ class: props.b24ui?.icon, checked: true })"
            />
            <Component
              :is="uncheckedIcon"
              v-if="uncheckedIcon"
              data-slot="icon"
              :class="b24ui.icon({ class: props.b24ui?.icon, unchecked: true })"
            />
          </template>
        </SwitchThumb>
      </SwitchRoot>
    </div>
    <div v-if="label || !!slots.label || (description || !!slots.description)" data-slot="wrapper" :class="b24ui.wrapper({ class: props.b24ui?.wrapper })">
      <Label v-if="label || !!slots.label" :for="id" data-slot="label" :class="b24ui.label({ class: props.b24ui?.label })">
        <slot name="label" :label="label">
          {{ label }}
        </slot>
      </Label>
      <p v-if="description || !!slots.description" data-slot="description" :class="b24ui.description({ class: props.b24ui?.description })">
        <slot name="description" :description="description">
          {{ description }}
        </slot>
      </p>
    </div>
  </Primitive>
</template>
