<script setup>
import { watch, onMounted } from "vue";
import { useCurrencyInput } from "vue-currency-input";
import { Input } from "@/Components/ui/input";
import { cn } from "@/lib/utils";

const props = defineProps({
  modelValue: { type: [Number, String, null], default: null },
  id: String,
  name: String,
  placeholder: { type: String, default: "0,00" },
  disabled: Boolean,
  required: Boolean,
  currency: { type: String, default: "EUR" },
  locale: { type: String, default: "sl-SI" },
  precision: { type: [Number, Object], default: 2 },
  allowNegative: { type: Boolean, default: false },
  useGrouping: { type: Boolean, default: true },
  class: { type: String, default: "" },
});

const emit = defineEmits(["update:modelValue", "change"]);

const { inputRef, numberValue, setValue, setOptions } = useCurrencyInput({
  currency: props.currency,
  locale: props.locale,
  precision: props.precision,
  useGrouping: props.useGrouping,
  valueRange: props.allowNegative ? {} : { min: 0 },
});

watch(
  () => props.modelValue,
  (val) => {
    const numeric = typeof val === "string" ? parseFloat(val) : val;
    if (numeric !== numberValue.value) {
      setValue(isNaN(numeric) ? null : numeric);
    }
  },
  { immediate: true }
);

watch(numberValue, (val) => {
  emit("update:modelValue", val);
});

watch(
  () => [
    props.currency,
    props.locale,
    props.precision,
    props.useGrouping,
    props.allowNegative,
  ],
  () => {
    setOptions({
      currency: props.currency,
      locale: props.locale,
      precision: props.precision,
      useGrouping: props.useGrouping,
      valueRange: props.allowNegative ? {} : { min: 0 },
    });
  }
);

onMounted(() => {
  if (props.modelValue != null) {
    const numeric =
      typeof props.modelValue === "string"
        ? parseFloat(props.modelValue)
        : props.modelValue;
    setValue(isNaN(numeric) ? null : numeric);
  }
});
</script>

<template>
  <input
    ref="inputRef"
    :id="id"
    :name="name"
    type="text"
    inputmode="decimal"
    :placeholder="placeholder"
    :disabled="disabled"
    :required="required"
    :class="cn('flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50', props.class)"
    autocomplete="off"
    @change="$emit('change', numberValue)"
  />
</template>
