import { ref as i, computed as D } from "vue";
import { defineStore as E, storeToRefs as _ } from "pinia";
import { s as o, O as M } from "./start-CE9SawgU.js";
import "vue-i18n";
import "./header-Bn1EKnBE.js";
import { t as P } from "./TaxZones-BAyb9A2u.js";
import { t as b } from "./TaxClasses-BZyo8A_n.js";
import { B as R } from "./base-BrZf5x6x.js";
const r = `${R}TaxRates.`, A = {
  /** getList */
  async getList(t) {
    return o.post(`${r}getList`, t);
  },
  /** get */
  async get(t) {
    return o.post(`${r}get`, {
      taxRate: { id: t }
    });
  },
  /** create */
  async create(t) {
    return o.post(`${r}create`, { taxRate: t });
  },
  /** save */
  async save(t) {
    return o.post(`${r}save`, { taxRate: t });
  },
  /** delete */
  async delete(t) {
    return o.post(`${r}delete`, {
      taxRate: { id: t }
    });
  },
  /** getTaxRateByTaxClassAndTaxZone */
  async getTaxRateByTaxClassAndTaxZone(t) {
    return o.post(
      `${r}getTaxRateByTaxClassAndTaxZone`,
      t
    );
  },
  /** findTaxRateAndTaxZone */
  async findTaxRateAndTaxZone(t) {
    return o.post(`${r}findTaxRateAndTaxZone`, t);
  },
  /** getListOfLanguagesScopesAndFields */
  async getListOfLanguagesScopesAndFields() {
    return o.post(
      `${r}getListOfLanguagesScopesAndFields`
    );
  }
}, T = `${R}MatchRules.`, I = {
  /** getList */
  async getList(t) {
    return o.post(`${T}getList`, t);
  },
  /** saveConfiguration */
  async saveConfiguration(t) {
    return o.post(`${T}saveConfiguration`, t);
  }
}, H = E("Taxes-dictionary", () => {
  const t = M(), { countries: L, isCountriesLoading: S, countryClassifications: $, currencies: h } = _(t), { ensureCountries: F, ensureCountryClassifications: y, ensureCurrencies: p } = t, s = i(!1), x = i({
    attributes: [],
    languages: [],
    scopes: []
  }), c = i([]), l = i([]), f = i([]), u = i([]), Z = D(
    () => l.value.map((a) => ({
      ...a,
      inUse: u.value.some(({ taxZone: e }) => e === a.id) || c.value.some(({ taxZone: e }) => e.id === a.id)
    }))
  );
  async function g(a = !0) {
    try {
      a && (s.value = !0), l.value = [];
      const { data: e } = await P.getList();
      l.value = e;
    } catch (e) {
      console.error("Failed to load taxZones", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function d(a = !0) {
    try {
      a && (s.value = !0), f.value = [];
      const { data: e } = await b.getList();
      f.value = e;
    } catch (e) {
      console.error("Failed to load taxClasses", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function O(a) {
    return await d(a);
  }
  async function v(a = !0) {
    try {
      a && (s.value = !0), u.value = [];
      const { data: e } = await I.getList();
      u.value = e.map(
        (n) => ({
          ...n,
          sourceCountryClassification: n.sourceCountryClassification?.id || null,
          sourceCountry: n.sourceCountry?.code3digit || null,
          destinationCountryClassification: n.destinationCountryClassification?.id || null,
          destinationCountry: n.destinationCountry?.code3digit || null,
          zipRuleType: n.zipRuleType || null,
          currency: n.currency?.id || null,
          taxZone: n.taxZone?.id || null,
          ustid: typeof n.ustid == "boolean" ? String(Number(n.ustid)) : null
        })
      );
    } catch (e) {
      console.error("Failed to load matchRules", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function C(a = !0) {
    try {
      a && (s.value = !0), x.value = await A.getListOfLanguagesScopesAndFields();
    } catch (e) {
      console.error("Failed to load ListOfLanguagesScopesAndFields", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function m(a = !0) {
    try {
      a && (s.value = !0), c.value = [];
      const { data: e } = await A.getList();
      c.value = e;
    } catch (e) {
      console.error("Failed to load taxRates", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function B() {
    try {
      return s.value = !0, await Promise.all([
        m(!1),
        g(!1),
        d(!1),
        v(!1),
        p(),
        F(),
        y(),
        C(!1)
      ]);
    } finally {
      s.value = !1;
    }
  }
  return {
    // state
    isLoading: s,
    countryClassifications: $,
    languagesScopesAndFields: x,
    taxRates: c,
    taxZones: l,
    taxClasses: f,
    matchRules: u,
    currencies: h,
    countries: L,
    isCountriesLoading: S,
    // getters
    getTaxZones: Z,
    // actions
    fetchTaxZones: g,
    fetchTaxClasses: d,
    ensureTaxClasses: O,
    fetchMatchRules: v,
    ensureCurrencies: p,
    ensureCountryClassifications: y,
    fetchListOfLanguagesScopesAndFields: C,
    fetchTaxRates: m,
    fetchAllData: B
  };
});
export {
  H as u
};
