import { u as w, r as l, Q as R, W as g } from "./app-Bpb4394e.js";
import { u as E } from "./supplier-Bu6V24R6.js";
import { computed as u } from "vue";
function d(o) {
  const c = g.safeParse(o);
  if (c.success) return c.data;
  throw new Error("Invalid supplier data");
}
function _(o) {
  return {
    _id: o.doc._id,
    internal: o.type,
    contactName: o.doc.profile.name,
    name: o.doc.profile.name ?? "",
    email: o.doc.profile.email,
    phoneNumber: o.doc.profile.phoneNo,
    registrationNumber: o.doc.profile.regNo,
    address: o.doc.profile.address ?? {
      line1: "",
      state: "",
      city: "",
      postcode: "",
      country: ""
    },
    preparationTime: o.preparationTime
  };
}
function F(o, c, v = !1, f = !1) {
  const p = E(), r = w();
  r.watchLocation(async () => {
    var a;
    await p.readSuppliers();
    const e = o();
    if ((a = e.supplier) != null && a._id) return;
    const s = f ? p.suppliers[0] : p.businessSuppliers[0], n = (v ? r.locations.filter((i) => i.doc._id !== r.currentLocation.doc._id) : r.locations)[0];
    c({
      ...e,
      supplier: s ? d(s) : n ? _(n) : e.supplier
    });
  });
  const y = u(
    () => {
      var e, s;
      return typeof ((s = (e = o()) == null ? void 0 : e.supplier) == null ? void 0 : s.internal) == "string" ? l.enum.transfer : l.enum.purchase;
    }
  ), h = u({
    get() {
      var e, s;
      return (s = (e = o()) == null ? void 0 : e.supplier) == null ? void 0 : s._id;
    },
    set(e) {
      const s = o() ?? {};
      if (y.value === l.enum.purchase) {
        const m = (f ? p.suppliers : p.businessSuppliers).find((N) => N._id === e);
        c({
          ...s,
          supplier: m ? d(m) : s.supplier
        });
        return;
      }
      const a = (v ? r.allLocations.filter(
        (i) => i.doc._id !== r.currentLocation.doc._id
      ) : r.allLocations).find((i) => i.doc._id === e);
      c({
        ...s,
        supplier: a ? _(a) : s.supplier
      });
    }
  }), L = u(() => p.locationSuppliers.map(d)), T = u(() => p.businessSuppliers.map(d)), S = u(() => {
    const e = [...L.value], s = [...T.value];
    return f ? [...s, ...e] : s;
  }), b = u(() => {
    var t;
    const e = ((t = r._currentLocation) == null ? void 0 : t.type) === R.warehouse;
    return (v ? r.allLocations.filter(
      (n) => n.doc._id !== r.currentLocation.doc._id
    ) : r.allLocations).filter((n) => e || n.acceptRequest).map(_);
  }), O = u(() => {
    if (y.value === l.enum.purchase) {
      const s = L.value.map((n) => ({
        label: n.name,
        value: n._id
      })), t = T.value.map((n) => ({
        label: n.name,
        sublabel: "Business supplier",
        value: n._id
      }));
      return f ? [...s, ...t] : t;
    }
    return b.value.map((s) => ({
      label: s.name,
      sublabel: s.internal ? `${s.internal}` : void 0,
      value: s._id
    }));
  }), P = u(() => {
    var n, a;
    const e = (a = (n = o()) == null ? void 0 : n.supplier) == null ? void 0 : a._id;
    if (y.value === l.enum.purchase) {
      const i = p.suppliers.find((m) => m._id === e);
      return i ? d(i) : null;
    }
    const t = r.allLocations.find((i) => i.doc._id);
    return t ? _(t) : null;
  });
  return {
    transferLocationModel: h,
    transferLocationOptions: O,
    locationOptionsTypePurchase: S,
    locationOptionsTypePurchaseLocationLevel: L,
    locationOptionsTypeTransfer: b,
    selectedLocation: P
  };
}
export {
  F as u
};
