import i18next from "i18next";
import { Notice } from "obsidian";
import { openSiteSelectionModal } from "../site-selection-modal";
import { openTagManagerModal } from "../modals/tag-manager-modal";
import { openCategoryManagerModal } from "../modals/category-manager-modal";
import type HaloPlugin from "../main";

export async function manageTags(plugin: HaloPlugin): Promise<void> {
  try {
    if (plugin.settings.sites.length === 0) {
      new Notice(i18next.t("command.pull_post.error_no_sites"));
      return;
    }

    let site = plugin.settings.sites[0];
    if (plugin.settings.sites.length > 1) {
      site = await openSiteSelectionModal(plugin);
    }

    openTagManagerModal(plugin, site);
  } catch (error) {
    console.error("[HaloPlugin] Manage tags failed:", error);
    new Notice(i18next.t("common.error_connection_failed"));
  }
}

export async function manageCategories(plugin: HaloPlugin): Promise<void> {
  try {
    if (plugin.settings.sites.length === 0) {
      new Notice(i18next.t("command.pull_post.error_no_sites"));
      return;
    }

    let site = plugin.settings.sites[0];
    if (plugin.settings.sites.length > 1) {
      site = await openSiteSelectionModal(plugin);
    }

    openCategoryManagerModal(plugin, site);
  } catch (error) {
    console.error("[HaloPlugin] Manage categories failed:", error);
    new Notice(i18next.t("common.error_connection_failed"));
  }
}