<template>
  <div class="column no-wrap" :class="attrs.class" :style="attrs.style">
    <div v-if="label || $slots.label" class="q-pl-xs">
      <slot name="label">
        <span>{{ label }}</span>
      </slot>
      <span v-if="required" class="text-negative q-ml-xs">*</span>
    </div>
    <q-select
      ref="selectRef"
      v-model="model"
      v-bind="selectAttrs"
      :error="!!error"
      :error-message="errorMessage"
      :rules
      hide-bottom-space
      class="input-violet"
      :class="inputClass"
      :popup-content-class="popupContentClass"
      @update:model-value="error = null"
    >
      <template v-for="(_, slotName) in $slots" #[slotName]="scope">
        <slot :name="slotName" v-bind="scope" />
      </template>
    </q-select>
  </div>
</template>

<script setup>
import { ref, onBeforeMount, useAttrs, computed } from "vue";

defineOptions({
  inheritAttrs: false,
});

const { label, inputClass, popupContentClass, rules } = defineProps({
  label: {
    type: String,
    default: "",
  },
  rules: {
    type: Array,
    default: () => [],
  },
  inputClass: {
    type: String,
    default: null,
  },
  popupContentClass: {
    type: String,
    default: null,
  },
});

const attrs = useAttrs();

const model = defineModel({ type: [String, Object, Array, Number, null] });
const error = defineModel("error", {
  type: [String, Object, Array, Boolean, null],
});

const selectRef = ref(null);
const required = ref(false);

const selectAttrs = computed(() => {
  // eslint-disable-next-line
  const { class: _, style: __, ...rest } = attrs;
  return rest;
});

const errorMessage = computed(() => {
  if (error.value == null) {
    return void 0;
  }
  if (typeof error.value === "boolean") {
    return void 0;
  }
  return String(error.value);
});

onBeforeMount(() => {
  rules.forEach((r) => {
    if (r?.$id === "required") return (required.value = true);
  });
});

defineExpose({
  selectRef,
});
</script>
