import type { NuxtApp } from '#app'

export const useFormater = (nuxtApp: NuxtApp) => {
  return new Formater(nuxtApp)
}

class Formater {
  private session
  constructor(nuxtApp: NuxtApp) {
    const { $session } = nuxtApp
    this.session = $session
  }

  price(p: number) {
    const formatdecimal = new Intl.NumberFormat(
      this.session.cookieData.value.country.iso_code,
      {
        style: 'decimal',
        maximumFractionDigits: this.session.cookieData.value.currency.precision,
        minimumFractionDigits: this.session.cookieData.value.currency.precision,
      },
    )
    if (this.session.cookieData.value.currency.suffix) {
      return this.session.cookieData.value.currency.sign + ' ' + formatdecimal.format(p)
    }
    else {
      return formatdecimal.format(p) + ' ' + this.session.cookieData.value.currency.sign
    }
  }
}
