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