import { Menu } from "@islands/KMenu/types.ts";
import { addMovieInfos } from "@islands/KMenu/commands/add_movie_infos.ts";
import { createNewMovie } from "@islands/KMenu/commands/create_movie.ts";
import { createNewArticle } from "@islands/KMenu/commands/create_article.ts";
import { getCookie } from "@lib/string.ts";
import { addSeriesInfo } from "@islands/KMenu/commands/add_series_infos.ts";
import { createNewSeries } from "@islands/KMenu/commands/create_series.ts";
import { updateAllRecommendations } from "@islands/KMenu/commands/create_recommendations.ts";
import { createNewRecipe } from "@islands/KMenu/commands/create_recipe.ts";

export const menus: Record<string, Menu> = {
  main: {
    title: "Run",
    entries: [
      {
        title: "Clear Cache",
        icon: "IconRefresh",
        cb: async (state) => {
          state.activeState.value = "loading";
          await fetch("/api/cache", {
            method: "DELETE",
          });
          state.activeState.value = "normal";
          state.visible.value = false;
        },
        visible: () => {
          if (!getCookie("session_cookie")) return false;
          return true;
        },
      },
      {
        title: "Login",
        icon: "IconLogin",
        cb: () => {
          const url = new URL(globalThis.location.href);
          url.pathname = "/api/auth/login";
          url.searchParams.set(
            "redirect",
            encodeURIComponent(globalThis.location.pathname),
          );
          globalThis.location.href = url.href;
        },
        visible: () => {
          return !getCookie("session_cookie");
        },
      },
      {
        title: "Search",
        icon: "IconSearch",
        cb: () => {
          globalThis.location.href += "?q=";
        },
        visible: () => {
          return !!getCookie("session_cookie") &&
            globalThis.location.search === "";
        },
      },
      {
        title: "Logout",
        icon: "IconLogout",
        cb: () => {
          const url = new URL(globalThis.location.href);
          url.pathname = "/api/auth/logout";
          url.searchParams.set(
            "redirect",
            encodeURIComponent(globalThis.location.pathname),
          );
          globalThis.location.href = url.href;
        },
        visible: () => {
          return !!getCookie("session_cookie");
        },
      },
      addSeriesInfo,
      createNewArticle,
      createNewMovie,
      createNewSeries,
      createNewRecipe,
      addMovieInfos,
      // updateAllRecommendations,
    ],
  },
};
