import { ref as i, computed as I } from "vue";
import { defineStore as U, storeToRefs as w } from "pinia";
import { o, L as N } from "./start-BYfL32Oe.mjs";
import "vue-i18n";
import "vue-router";
import "./header-Droxy2Kd.mjs";
import "./moment-h96o7c8I.mjs";
import { t as j } from "./TaxZones-DQi5U-g7.mjs";
import { t as k } from "./TaxClasses-BY8s9nMy.mjs";
import { B as Z } from "./base-Cy9pA4ID.mjs";
const r = `${Z}TaxRates.`, h = {
  /** 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`
    );
  }
}, F = `${Z}MatchRules.`, q = {
  /** getList */
  async getList(t) {
    return o.post(`${F}getList`, t);
  },
  /** saveConfiguration */
  async saveConfiguration(t) {
    return o.post(`${F}saveConfiguration`, t);
  }
}, ee = U("Taxes-dictionary", () => {
  const t = N(), { countries: O, isCountriesLoading: B, countryClassifications: D, currencies: E } = w(t), { ensureCountries: _, ensureCountryClassifications: y, ensureCurrencies: p } = t, s = i(!1), x = i({
    attributes: [],
    languages: [],
    scopes: []
  }), c = i([]), l = i([]), f = i([]), u = i([]), M = I(
    () => 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 j.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 k.getList();
      f.value = e;
    } catch (e) {
      console.error("Failed to load taxClasses", e);
    } finally {
      a && (s.value = !1);
    }
  }
  async function P(a) {
    return await d(a);
  }
  async function v(a = !0) {
    try {
      a && (s.value = !0), u.value = [];
      const { data: e } = await q.getList();
      u.value = e.map(
        (n) => {
          var A, T, R, L, S, $;
          return {
            ...n,
            sourceCountryClassification: ((A = n.sourceCountryClassification) == null ? void 0 : A.id) || null,
            sourceCountry: ((T = n.sourceCountry) == null ? void 0 : T.code3digit) || null,
            destinationCountryClassification: ((R = n.destinationCountryClassification) == null ? void 0 : R.id) || null,
            destinationCountry: ((L = n.destinationCountry) == null ? void 0 : L.code3digit) || null,
            zipRuleType: n.zipRuleType || null,
            currency: ((S = n.currency) == null ? void 0 : S.id) || null,
            taxZone: (($ = n.taxZone) == null ? void 0 : $.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 h.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 h.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(),
        _(),
        y(),
        C(!1)
      ]);
    } finally {
      s.value = !1;
    }
  }
  return {
    // state
    isLoading: s,
    countryClassifications: D,
    languagesScopesAndFields: x,
    taxRates: c,
    taxZones: l,
    taxClasses: f,
    matchRules: u,
    currencies: E,
    countries: O,
    isCountriesLoading: B,
    // getters
    getTaxZones: M,
    // actions
    fetchTaxZones: g,
    fetchTaxClasses: d,
    ensureTaxClasses: P,
    fetchMatchRules: v,
    ensureCurrencies: p,
    ensureCountryClassifications: y,
    fetchListOfLanguagesScopesAndFields: C,
    fetchTaxRates: m,
    fetchAllData: b
  };
});
export {
  ee as u
};
