import { defineConfig } from "sanity";
import { structureTool } from "sanity/structure";
import { schemaTypes, singletonTypes } from "./schemas";

// Every page is a singleton (exactly one document). Listed here in nav order.
// Keep in sync with singletonTypes in ./schemas.
const SINGLETONS = [
  { id: "homePage", title: "Home page" },
  { id: "aboutPage", title: "About page" },
  { id: "eventsPage", title: "Events page" },
  { id: "membershipPage", title: "Membership page" },
  { id: "uefAngelsPage", title: "UEF Angels page" },
  { id: "youthWingPage", title: "Youth Wing page" },
  { id: "contactPage", title: "Contact page" },
  { id: "institutionsPage", title: "Institutions page" },
];

/**
 * The editing UI the UEF team uses. Deployed to a Sanity-hosted URL via
 * `npm run deploy` (https://uef.sanity.studio) — they log in, edit, Publish;
 * a webhook then revalidates the website.
 */
export default defineConfig({
  name: "default",
  title: "United Economic Forum",

  projectId: "lrlnd56q",
  dataset: "production",

  plugins: [
    structureTool({
      structure: (S) =>
        S.list()
          .title("Content")
          .items([
            ...SINGLETONS.map((s) =>
              S.listItem()
                .title(s.title)
                .id(s.id)
                .child(S.document().schemaType(s.id).documentId(s.id)),
            ),
            S.divider(),
            ...S.documentTypeListItems().filter(
              (li) => !singletonTypes.includes(li.getId() ?? ""),
            ),
          ]),
    }),
  ],

  schema: {
    types: schemaTypes,
  },

  // Singletons can't be created or deleted from the UI — only edited.
  document: {
    actions: (prev, { schemaType }) =>
      singletonTypes.includes(schemaType)
        ? prev.filter(({ action }) =>
            ["publish", "discardChanges", "restore"].includes(action ?? ""),
          )
        : prev,
  },
});
