import { u as i } from "./stock-DWkfMCtv.js";
import { a as s, O as p } from "./app-Bpb4394e.js";
import { ref as k, computed as B } from "vue";
import { c as d } from "./number-BVg7EKPV.js";
function m() {
  return {
    stockBalance: {
      _id: "",
      items: []
    },
    productBalance: []
  };
}
const v = d(0), f = {
  id: "",
  balance: v,
  closing: null,
  costs: null
}, n = k(m()), w = () => {
  const r = s(), u = B(() => {
    var a;
    const o = (a = n.value.stockBalance.skus) == null ? void 0 : a.groupBy((t) => t.id), e = (t) => {
      var c;
      return ((c = o == null ? void 0 : o.get(t)) == null ? void 0 : c[0]) ?? { ...f, id: t };
    };
    return r.skus.map((t) => e(t._id));
  }), l = i();
  return {
    // state
    get stockBalance() {
      return {
        ...n.value.stockBalance,
        skus: u.value
      };
    },
    set stockBalance(o) {
      n.value.stockBalance = o;
    },
    get productBalance() {
      return n.value.productBalance;
    },
    set productBalance(o) {
      n.value.productBalance = o;
    },
    // actions
    async readStockBalance() {
      this.stockBalance = await l.readStockBalance();
    },
    async readProductBalance() {
      this.productBalance = [];
      const o = s(), e = p({
        recipes: o.recipes,
        skus: o.skus,
        skuGroups: o.skuGroups,
        inventoryBindingMapping: {},
        stockBalance: this.stockBalance
        // silent: true
      });
      function a(t, c) {
        return Object.prototype.hasOwnProperty.call(t, c);
      }
      for (const t in e.convertCountdown)
        if (a(e.convertCountdown, t)) {
          const c = e.convertCountdown[t];
          this.productBalance.push({
            id: t,
            count: c
          });
        }
    }
  };
};
export {
  w as u
};
