import {defineConfig} from "sanity";
import {deskTool} from "sanity/desk";
import {schemaTypes} from "./schemas";
import {structure} from "./structure";
import {createThemePublishAction} from "./document-actions/theme-publish-action";
import {createThemeDeleteAction} from "./document-actions/theme-delete-action";

const projectId = process.env.SANITY_STUDIO_PROJECT_ID;
const dataset = process.env.SANITY_STUDIO_DATASET || "production";

if (!projectId) {
  throw new Error(
    "Missing SANITY_STUDIO_PROJECT_ID. Set it in apps/cms/.env and restart `npm run dev:cms`."
  );
}

export default defineConfig({
  name: "default",
  title: "Stylunique CMS",
  projectId,
  dataset,
  plugins: [deskTool({structure})],
  document: {
    actions: (previousActions, context) => {
      if (context.schemaType !== "theme") {
        return previousActions;
      }

      return previousActions.map((action) => {
        if (action.action === "publish") {
          return createThemePublishAction(action);
        }

        if (action.action === "delete") {
          return createThemeDeleteAction(action);
        }

        return action;
      });
    },
  },
  schema: {
    types: schemaTypes,
  },
});


