import {
  getApiKeyStatusProps,
  getApiKeyTypeFromPathname,
  getApiKeyTypeProps,
  prettifyRedactedToken
} from "./chunk-XFR5JLNF.js";
import {
  DateCell
} from "./chunk-25LYVP3C.js";
import {
  TextCell
} from "./chunk-JHIMVLB2.js";
import {
  StatusCell
} from "./chunk-DB6ANJBO.js";
import {
  _DataTable,
  useDataTable
} from "./chunk-ULJ3OMWU.js";
import {
  SingleColumnPage
} from "./chunk-XIZLPYEE.js";
import {
  ActionMenu
} from "./chunk-BDGZ4EQO.js";
import {
  useQueryParams
} from "./chunk-THHRRYRS.js";
import {
  useApiKeys,
  useDeleteApiKey,
  useRevokeApiKey
} from "./chunk-YNIIYGL4.js";

// src/pages/api-key-management/api-key-management-list/api-key-management-list.tsx
import { Children as Children3 } from "react";

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-view/api-key-management-list-view.tsx
import { Children as Children2 } from "react";
import { Container } from "@medusajs/ui";

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-view/api-key-management-list-data-table.tsx
import { keepPreviousData } from "@tanstack/react-query";
import { useTranslation as useTranslation4 } from "react-i18next";
import { useLocation } from "react-router-dom";

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-table/use-api-key-management-table-columns.tsx
import { Badge } from "@medusajs/ui";
import { createColumnHelper } from "@tanstack/react-table";
import { useMemo } from "react";
import { useTranslation as useTranslation2 } from "react-i18next";

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-table/api-key-row-actions.tsx
import { PencilSquare, SquareTwoStack, Trash, XCircle } from "@medusajs/icons";
import { toast, usePrompt } from "@medusajs/ui";
import { useTranslation } from "react-i18next";
import { jsx } from "react/jsx-runtime";
var ApiKeyRowActions = ({
  apiKey
}) => {
  const { mutateAsync: revokeAsync } = useRevokeApiKey(apiKey.id);
  const { mutateAsync: deleteAsync } = useDeleteApiKey(apiKey.id);
  const { t } = useTranslation();
  const prompt = usePrompt();
  const handleDelete = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("apiKeyManagement.delete.warning", {
        title: apiKey.title
      }),
      confirmText: t("actions.delete"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await deleteAsync(void 0, {
      onSuccess: () => {
        toast.success(
          t("apiKeyManagement.delete.successToast", {
            title: apiKey.title
          })
        );
      },
      onError: (err) => {
        toast.error(err.message);
      }
    });
  };
  const handleRevoke = async () => {
    const res = await prompt({
      title: t("general.areYouSure"),
      description: t("apiKeyManagement.revoke.warning", {
        title: apiKey.title
      }),
      confirmText: t("apiKeyManagement.actions.revoke"),
      cancelText: t("actions.cancel")
    });
    if (!res) {
      return;
    }
    await revokeAsync(void 0, {
      onSuccess: () => {
        toast.success(
          t("apiKeyManagement.revoke.successToast", {
            title: apiKey.title
          })
        );
      },
      onError: (err) => {
        toast.error(err.message);
      }
    });
  };
  const handleCopyToken = () => {
    navigator.clipboard.writeText(apiKey.token);
    toast.success(t("apiKeyManagement.actions.copySuccessToast"));
  };
  return /* @__PURE__ */ jsx(
    ActionMenu,
    {
      groups: [
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(PencilSquare, {}),
              label: t("actions.edit"),
              to: `${apiKey.id}/edit`
            },
            ...apiKey.type !== "secret" ? [
              {
                label: t("apiKeyManagement.actions.copy"),
                onClick: handleCopyToken,
                icon: /* @__PURE__ */ jsx(SquareTwoStack, {})
              }
            ] : []
          ]
        },
        {
          actions: [
            {
              icon: /* @__PURE__ */ jsx(XCircle, {}),
              label: t("apiKeyManagement.actions.revoke"),
              onClick: handleRevoke,
              disabled: !!apiKey.revoked_at
            },
            {
              icon: /* @__PURE__ */ jsx(Trash, {}),
              label: t("actions.delete"),
              onClick: handleDelete,
              disabled: !apiKey.revoked_at
            }
          ]
        }
      ],
      "data-testid": `api-key-row-action-menu-${apiKey.id}`
    }
  );
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-table/use-api-key-management-table-columns.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var columnHelper = createColumnHelper();
var useApiKeyManagementTableColumns = () => {
  const { t } = useTranslation2();
  return useMemo(
    () => [
      columnHelper.accessor("title", {
        header: t("fields.title"),
        cell: ({ getValue }) => /* @__PURE__ */ jsx2("div", { className: "flex size-full items-center", children: /* @__PURE__ */ jsx2("span", { className: "truncate", children: getValue() }) })
      }),
      columnHelper.accessor("redacted", {
        header: "Token",
        cell: ({ getValue }) => {
          const token = getValue();
          return /* @__PURE__ */ jsx2(Badge, { size: "2xsmall", children: prettifyRedactedToken(token) });
        }
      }),
      columnHelper.accessor("type", {
        header: t("fields.type"),
        cell: ({ getValue }) => {
          const { label } = getApiKeyTypeProps(getValue(), t);
          return /* @__PURE__ */ jsx2(TextCell, { text: label });
        }
      }),
      columnHelper.accessor("revoked_at", {
        header: t("fields.status"),
        cell: ({ getValue }) => {
          const { color, label } = getApiKeyStatusProps(getValue(), t);
          return /* @__PURE__ */ jsx2(StatusCell, { color, children: label });
        }
      }),
      columnHelper.accessor("last_used_at", {
        header: t("apiKeyManagement.table.lastUsedAtHeader"),
        cell: ({ getValue }) => {
          const date = getValue();
          return /* @__PURE__ */ jsx2(DateCell, { date });
        }
      }),
      columnHelper.accessor("created_at", {
        header: t("fields.created"),
        cell: ({ getValue }) => {
          const date = getValue();
          return /* @__PURE__ */ jsx2(DateCell, { date });
        }
      }),
      columnHelper.display({
        id: "actions",
        cell: ({ row }) => {
          return /* @__PURE__ */ jsx2(ApiKeyRowActions, { apiKey: row.original });
        }
      })
    ],
    [t]
  );
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-table/use-api-key-management-table-filters.tsx
import { useTranslation as useTranslation3 } from "react-i18next";
var useApiKeyManagementTableFilters = () => {
  const { t } = useTranslation3();
  let filters = [];
  const dateFilters = [
    { label: t("fields.createdAt"), key: "created_at", type: "date" },
    { label: t("fields.updatedAt"), key: "updated_at", type: "date" },
    { label: t("fields.revokedAt"), key: "revoked_at", type: "date" }
  ];
  filters = [...filters, ...dateFilters];
  return filters;
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-table/use-api-key-management-table-query.tsx
var useApiKeyManagementTableQuery = ({
  prefix,
  pageSize = 20
}) => {
  const queryObject = useQueryParams(
    ["offset", "q", "created_at", "updated_at", "revoked_at", "order"],
    prefix
  );
  const { offset, created_at, updated_at, revoked_at, q, order } = queryObject;
  const searchParams = {
    limit: pageSize,
    offset: offset ? Number(offset) : 0,
    created_at: created_at ? JSON.parse(created_at) : void 0,
    updated_at: updated_at ? JSON.parse(updated_at) : void 0,
    revoked_at: revoked_at ? JSON.parse(revoked_at) : void 0,
    order,
    q
  };
  return {
    searchParams,
    raw: queryObject
  };
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-view/api-key-management-list-data-table.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var PAGE_SIZE = 20;
var ApiKeyManagementListDataTable = () => {
  const { t } = useTranslation4();
  const { pathname } = useLocation();
  const keyType = getApiKeyTypeFromPathname(pathname);
  const { searchParams, raw } = useApiKeyManagementTableQuery({
    pageSize: PAGE_SIZE
  });
  const query = {
    ...searchParams,
    type: keyType,
    fields: "id,title,redacted,token,type,created_at,updated_at,revoked_at,last_used_at,created_by,revoked_by"
  };
  const { api_keys, count, isLoading, isError, error } = useApiKeys(
    query,
    {
      placeholderData: keepPreviousData
    }
  );
  const filters = useApiKeyManagementTableFilters();
  const columns = useApiKeyManagementTableColumns();
  const { table } = useDataTable({
    data: api_keys || [],
    columns,
    count,
    enablePagination: true,
    getRowId: (row) => row.id,
    pageSize: PAGE_SIZE
  });
  if (isError) {
    throw error;
  }
  return /* @__PURE__ */ jsx3(
    _DataTable,
    {
      table,
      filters,
      columns,
      count,
      pageSize: PAGE_SIZE,
      orderBy: [
        { key: "title", label: t("fields.title") },
        { key: "created_at", label: t("fields.createdAt") },
        { key: "updated_at", label: t("fields.updatedAt") },
        { key: "revoked_at", label: t("fields.revokedAt") }
      ],
      navigateTo: (row) => row.id,
      pagination: true,
      search: true,
      queryObject: raw,
      isLoading
    }
  );
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-view/api-key-management-list-header.tsx
import { Children } from "react";
import { Button, Heading, Text } from "@medusajs/ui";
import { useTranslation as useTranslation5 } from "react-i18next";
import { Link, useLocation as useLocation2 } from "react-router-dom";
import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
var ApiKeyManagementListTitle = () => {
  const { t } = useTranslation5();
  const { pathname } = useLocation2();
  const keyType = getApiKeyTypeFromPathname(pathname);
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsx4(Heading, { level: "h2", "data-testid": `${keyType}-api-keys-heading`, children: keyType === "publishable" ? t(`apiKeyManagement.domain.publishable`) : t("apiKeyManagement.domain.secret") }),
    /* @__PURE__ */ jsx4(
      Text,
      {
        className: "text-ui-fg-subtle",
        size: "small",
        "data-testid": `${keyType}-api-keys-description`,
        children: keyType === "publishable" ? t(`apiKeyManagement.subtitle.publishable`) : t("apiKeyManagement.subtitle.secret")
      }
    )
  ] });
};
var ApiKeyManagementListActions = ({
  children
}) => {
  const { t } = useTranslation5();
  const { pathname } = useLocation2();
  const keyType = getApiKeyTypeFromPathname(pathname);
  return /* @__PURE__ */ jsx4("div", { className: "flex items-center justify-center gap-x-2", children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsx4(Link, { to: "create", "data-testid": `${keyType}-api-keys-create-button`, children: /* @__PURE__ */ jsx4(Button, { variant: "secondary", size: "small", children: t("actions.create") }) }) });
};
var ApiKeyManagementListHeader = ({
  children
}) => {
  const { pathname } = useLocation2();
  const keyType = getApiKeyTypeFromPathname(pathname);
  return /* @__PURE__ */ jsx4(
    "div",
    {
      className: "flex items-center justify-between px-6 py-4",
      "data-testid": `${keyType}-api-keys-header`,
      children: Children.count(children) > 0 ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx4(ApiKeyManagementListTitle, {}),
        /* @__PURE__ */ jsx4(ApiKeyManagementListActions, {})
      ] })
    }
  );
};

// src/pages/api-key-management/api-key-management-list/components/api-key-management-list-view/api-key-management-list-view.tsx
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
var ApiKeyManagementListView = ({
  children
}) => {
  return /* @__PURE__ */ jsx5(Container, { className: "divide-y p-0", children: Children2.count(children) > 0 ? children : /* @__PURE__ */ jsxs2(Fragment2, { children: [
    /* @__PURE__ */ jsx5(ApiKeyManagementListHeader, {}),
    /* @__PURE__ */ jsx5(ApiKeyManagementListDataTable, {})
  ] }) });
};

// src/pages/api-key-management/api-key-management-list/api-key-management-list.tsx
import { jsx as jsx6 } from "react/jsx-runtime";
var Root = ({ children }) => {
  return /* @__PURE__ */ jsx6(
    SingleColumnPage,
    {
      hasOutlet: true,
      "data-testid": "publishable-api-keys-list-page",
      children: Children3.count(children) > 0 ? children : /* @__PURE__ */ jsx6(ApiKeyManagementListView, {})
    }
  );
};
var ApiKeyManagementListPage = Object.assign(Root, {
  Table: ApiKeyManagementListView,
  Header: ApiKeyManagementListHeader,
  HeaderTitle: ApiKeyManagementListTitle,
  HeaderActions: ApiKeyManagementListActions,
  DataTable: ApiKeyManagementListDataTable
});

export {
  ApiKeyManagementListPage
};
