<script setup lang="ts">
import type { ColumnType, UITypes } from 'nocodb-sdk'
import { AllowedColumnTypesForQrAndBarcodes } from 'nocodb-sdk'

const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue'])

const { t } = useI18n()

const { fields, metaColumnById } = useViewColumnsOrThrow()

const vModel = useVModel(props, 'modelValue', emit)

const { setAdditionalValidations, setAvoidShowingToastMsgForValidations, validateInfos, column, isEdit } =
  useColumnCreateStoreOrThrow()

const columnsAllowedAsQrValue = computed<ColumnType[]>(() => {
  return (
    fields.value
      ?.filter(
        (el) =>
          el.fk_column_id && AllowedColumnTypesForQrAndBarcodes.includes(metaColumnById.value[el.fk_column_id].uidt as UITypes),
      )
      .map((field) => {
        return metaColumnById.value[field.fk_column_id!]
      }) || []
  )
})

onMounted(() => {
  // set default value
  vModel.value.fk_qr_value_column_id =
    (column?.value?.colOptions as Record<string, any>)?.fk_qr_value_column_id ||
    (!isEdit.value ? columnsAllowedAsQrValue.value?.[0]?.id : null)
})

setAdditionalValidations({
  fk_qr_value_column_id: [{ required: true, message: t('general.required') }],
})

setAvoidShowingToastMsgForValidations({
  fk_qr_value_column_id: true,
})
</script>

<template>
  <div class="flex flex-col gap-2">
    <a-form-item
      class="flex nc-qr-code-value-column-select"
      :label="`${$t('placeholder.value')} ${t('objects.field').toLowerCase()}`"
      v-bind="validateInfos.fk_qr_value_column_id"
    >
      <a-select
        v-model:value="vModel.fk_qr_value_column_id"
        :placeholder="$t('placeholder.selectAColumnForTheQRCodeValue')"
        @click.stop
      >
        <template #suffixIcon>
          <GeneralIcon icon="arrowDown" class="text-nc-content-gray-subtle" />
        </template>

        <a-select-option v-for="(option, index) of columnsAllowedAsQrValue" :key="index" :value="option.id">
          <div class="flex gap-2 w-full truncate items-center" :data-testid="`nc-qr-${option.title}`">
            <div class="inline-flex items-center gap-2 flex-1 truncate">
              <SmartsheetHeaderIcon :column="option" class="!mx-0 w-4 h-4" color="text-nc-content-gray-subtle2" />

              <NcTooltip show-on-truncate-only class="flex-1 truncate">
                <template #title>{{ option.title }}</template>
                {{ option.title }}
              </NcTooltip>
            </div>

            <component
              :is="iconMap.check"
              v-if="vModel.fk_qr_value_column_id === option.id"
              id="nc-selected-item-icon"
              class="text-primary w-4 h-4"
            />
          </div>
        </a-select-option>
      </a-select>
    </a-form-item>
  </div>
</template>
