import { match, P } from "ts-pattern";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
const fetchProviders = async () => {
  const call = await apiCall(
    "/api/shipping-carriers/providers",
    void 0,
    { fallback: { providers: [] } }
  );
  return match(call).with({ ok: true, result: P.not(P.nullish) }, ({ result }) => ({
    ok: true,
    providers: result.providers
  })).otherwise(() => ({ ok: false, error: "Failed to load shipping providers." }));
};
const fetchOrderAddresses = async (orderId) => {
  const call = await apiCall(
    `/api/sales/document-addresses?documentId=${orderId}&documentKind=order&pageSize=50`,
    void 0,
    { fallback: { items: [] } }
  );
  return match(call).with({ ok: true, result: P.not(P.nullish) }, ({ result }) => ({
    ok: true,
    items: result.items
  })).otherwise(() => ({ ok: false }));
};
const fetchRates = async (params) => {
  const { providerKey, origin, destination, packages, receiverPhone, receiverEmail } = params;
  const call = await apiCall(
    "/api/shipping-carriers/rates",
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        providerKey,
        origin,
        destination,
        packages,
        ...receiverPhone !== void 0 ? { receiverPhone } : {},
        ...receiverEmail !== void 0 ? { receiverEmail } : {}
      })
    },
    { fallback: { rates: [] } }
  );
  return match(call).with({ ok: true, result: P.not(P.nullish) }, ({ result }) => ({
    ok: true,
    rates: result.rates
  })).otherwise(({ result }) => ({
    ok: false,
    error: result?.error ?? "Failed to fetch shipping rates."
  }));
};
const createShipment = async (params) => {
  const call = await apiCall(
    "/api/shipping-carriers/shipments",
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(params)
    },
    { fallback: null }
  );
  return match(call).with({ ok: true }, () => ({ ok: true })).otherwise(({ result }) => ({
    ok: false,
    error: result?.error ?? "Failed to create shipment.",
    code: result?.code
  }));
};
const fetchDropOffPoints = async (params) => {
  const url = new URL("/api/shipping-carriers/points", "http://placeholder");
  url.searchParams.set("providerKey", params.providerKey);
  if (params.query) url.searchParams.set("query", params.query);
  if (params.type) url.searchParams.set("type", params.type);
  if (params.postCode) url.searchParams.set("postCode", params.postCode);
  const call = await apiCall(
    `${url.pathname}${url.search}`,
    void 0,
    { fallback: { points: [] } }
  );
  return match(call).with({ ok: true, result: P.not(P.nullish) }, ({ result }) => ({
    ok: true,
    points: result.points
  })).otherwise(({ result }) => ({
    ok: false,
    error: result?.error ?? "Failed to fetch drop-off points."
  }));
};
export {
  createShipment,
  fetchDropOffPoints,
  fetchOrderAddresses,
  fetchProviders,
  fetchRates
};
//# sourceMappingURL=shipmentApi.js.map
