import {
  LinkButton
} from "./chunk-7AV5EJJ5.js";
import {
  getFormattedAddress
} from "./chunk-W6Y3PPU5.js";
import {
  SidebarLink
} from "./chunk-4GPAS5GW.js";
import {
  TwoColumnPage
} from "./chunk-YV6MK4PZ.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  stockLocationsQueryKeys,
  useDeleteStockLocation,
  useStockLocations
} from "./chunk-O72AIZUN.js";
import {
  queryClient
} from "./chunk-RHKRREUU.js";
import {
  fetchQuery
} from "./chunk-DTGEFKK4.js";

// src/pages/settings/locations/constants.ts
var LOCATION_LIST_FIELDS = "name,*sales_channels,*address,*fulfillment_sets,*fulfillment_sets.service_zones,*fulfillment_sets.service_zones.shipping_options,*fulfillment_sets.service_zones.shipping_options.shipping_profile";

// src/pages/settings/locations/loader.ts
var shippingListQuery = () => ({
  queryKey: stockLocationsQueryKeys.lists(),
  queryFn: async () => {
    return await fetchQuery("/vendor/stock-locations", {
      method: "GET",
      query: { fields: LOCATION_LIST_FIELDS }
    });
  }
});
var shippingListLoader = async (_) => {
  const query = shippingListQuery();
  return queryClient.getQueryData(
    query.queryKey
  ) ?? await queryClient.fetchQuery(query);
};

// src/pages/settings/locations/location-list-page.tsx
import { ShoppingBag } from "@medusajs/icons";
import { Container as Container3, Heading as Heading2 } from "@medusajs/ui";
import { Children } from "react";
import { useTranslation as useTranslation3 } from "react-i18next";
import { useLoaderData } from "react-router-dom";
import { WidgetZone } from "@mercurjs/dashboard-shared";

// src/pages/settings/locations/_components/location-list-item.tsx
import { Buildings, PencilSquare, Trash } from "@medusajs/icons";
import { Container, StatusBadge, Text, toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { jsx, jsxs } from "react/jsx-runtime";
function FulfillmentSet(props) {
  const { t } = useTranslation();
  const { fulfillmentSet, type } = props;
  const fulfillmentSetExists = !!fulfillmentSet;
  return /* @__PURE__ */ jsx("div", { className: "flex flex-col px-6 py-4", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
    /* @__PURE__ */ jsx(
      Text,
      {
        size: "small",
        weight: "plus",
        className: "text-ui-fg-subtle flex-1",
        as: "div",
        children: t(`stockLocations.fulfillmentSets.${type}.header`)
      }
    ),
    /* @__PURE__ */ jsx("div", { className: "flex-1 text-left", children: /* @__PURE__ */ jsx(StatusBadge, { color: fulfillmentSetExists ? "green" : "grey", children: t(fulfillmentSetExists ? "statuses.enabled" : "statuses.disabled") }) })
  ] }) });
}
function LocationListItem(props) {
  const { location } = props;
  const { t } = useTranslation();
  const prompt = usePrompt();
  const { mutateAsync: deleteLocation } = useDeleteStockLocation(location.id);
  const handleDelete = async () => {
    const result = await prompt({
      title: t("general.areYouSure"),
      description: t("stockLocations.delete.confirmation", {
        name: location.name
      }),
      confirmText: t("actions.remove"),
      cancelText: t("actions.cancel")
    });
    if (!result) {
      return;
    }
    await deleteLocation(void 0, {
      onSuccess: () => {
        toast.success(
          t("shippingProfile.delete.successToast", {
            name: location.name
          })
        );
      },
      onError: (e) => {
        toast.error(e.message);
      }
    });
  };
  return /* @__PURE__ */ jsxs(Container, { className: "flex flex-col divide-y p-0", children: [
    /* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-row items-center justify-between gap-x-4", children: [
      /* @__PURE__ */ jsx("div", { className: "shadow-borders-base flex size-7 items-center justify-center rounded-md", children: /* @__PURE__ */ jsx("div", { className: "bg-ui-bg-field flex size-6 items-center justify-center rounded-[4px]", children: /* @__PURE__ */ jsx(Buildings, { className: "text-ui-fg-subtle" }) }) }),
      /* @__PURE__ */ jsxs("div", { className: "grow-1 flex flex-1 flex-col", children: [
        /* @__PURE__ */ jsx(Text, { weight: "plus", children: location.name }),
        /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle txt-small", children: getFormattedAddress({
          address: {
            company: location.address?.company,
            address_1: location.address?.address_1,
            address_2: location.address?.address_2,
            city: location.address?.city,
            postal_code: location.address?.postal_code,
            province: location.address?.province,
            country_code: location.address?.country_code
          }
        }).join(", ") })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "flex grow-0 items-center gap-4", children: [
        /* @__PURE__ */ jsx(
          ActionMenu,
          {
            groups: [
              {
                actions: [
                  {
                    label: t("actions.edit"),
                    icon: /* @__PURE__ */ jsx(PencilSquare, {}),
                    to: `/settings/locations/${location.id}/edit`
                  }
                ]
              },
              {
                actions: [
                  {
                    label: t("actions.delete"),
                    icon: /* @__PURE__ */ jsx(Trash, {}),
                    onClick: handleDelete
                  }
                ]
              }
            ]
          }
        ),
        /* @__PURE__ */ jsx("div", { className: "bg-ui-border-strong h-[12px] w-[1px]" }),
        /* @__PURE__ */ jsx(LinkButton, { to: `/settings/locations/${location.id}`, children: t("actions.viewDetails") })
      ] })
    ] }) }),
    /* @__PURE__ */ jsx(
      FulfillmentSet,
      {
        type: "pickup" /* Pickup */,
        fulfillmentSet: location.fulfillment_sets?.find(
          (f) => f.type === "pickup" /* Pickup */
        )
      }
    ),
    /* @__PURE__ */ jsx(
      FulfillmentSet,
      {
        type: "shipping" /* Shipping */,
        fulfillmentSet: location.fulfillment_sets?.find(
          (f) => f.type === "shipping" /* Shipping */
        )
      }
    )
  ] });
}
var location_list_item_default = LocationListItem;

// src/pages/settings/locations/_components/location-list-header.tsx
import { Button, Container as Container2, Heading, Text as Text2 } from "@medusajs/ui";
import { useTranslation as useTranslation2 } from "react-i18next";
import { Link } from "react-router-dom";
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var LocationListHeader = () => {
  const { t } = useTranslation2();
  return /* @__PURE__ */ jsxs2(Container2, { className: "flex h-fit items-center justify-between gap-x-4 px-6 py-4", children: [
    /* @__PURE__ */ jsxs2("div", { children: [
      /* @__PURE__ */ jsx2(Heading, { children: t("stockLocations.domain") }),
      /* @__PURE__ */ jsx2(Text2, { className: "text-ui-fg-subtle txt-small", children: t("stockLocations.list.description") })
    ] }),
    /* @__PURE__ */ jsx2(Button, { size: "small", className: "shrink-0", variant: "secondary", asChild: true, children: /* @__PURE__ */ jsx2(Link, { to: "create", children: t("actions.create") }) })
  ] });
};

// src/pages/settings/locations/location-list-page.tsx
import { Fragment, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
var LinksSection = () => {
  const { t } = useTranslation3();
  return /* @__PURE__ */ jsxs3(Container3, { className: "p-0", children: [
    /* @__PURE__ */ jsx3("div", { className: "flex items-center justify-between px-6 py-4", children: /* @__PURE__ */ jsx3(Heading2, { level: "h2", children: t("stockLocations.sidebar.header") }) }),
    /* @__PURE__ */ jsx3(
      SidebarLink,
      {
        to: "/settings/locations/shipping-profiles",
        labelKey: t("stockLocations.sidebar.shippingProfiles.label"),
        descriptionKey: t(
          "stockLocations.sidebar.shippingProfiles.description"
        ),
        icon: /* @__PURE__ */ jsx3(ShoppingBag, {})
      }
    )
  ] });
};
var Root = ({ children }) => {
  const initialData = useLoaderData();
  const {
    stock_locations: stockLocations = [],
    isError,
    error
  } = useStockLocations(
    {
      fields: LOCATION_LIST_FIELDS
    },
    // @ts-expect-error
    { initialData }
  );
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(Fragment, { children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs3(TwoColumnPage, { children: [
    /* @__PURE__ */ jsx3(TwoColumnPage.Main, { children: /* @__PURE__ */ jsxs3(WidgetZone, { id: "locations.list", children: [
      /* @__PURE__ */ jsx3(LocationListHeader, {}),
      /* @__PURE__ */ jsx3("div", { className: "flex flex-col gap-3 lg:col-span-2", children: stockLocations.map((location) => /* @__PURE__ */ jsx3(location_list_item_default, { location }, location.id)) })
    ] }) }),
    /* @__PURE__ */ jsx3(TwoColumnPage.Sidebar, { children: /* @__PURE__ */ jsx3(LinksSection, {}) })
  ] }) });
};
var LocationListPage = Object.assign(Root, {
  Main: TwoColumnPage.Main,
  Sidebar: TwoColumnPage.Sidebar,
  Header: LocationListHeader,
  ListItem: location_list_item_default,
  SidebarLinksSection: LinksSection
});

export {
  shippingListLoader,
  LocationListPage
};
