import i18next from "i18next";
import { Notice } from "obsidian";
import { openSiteSelectionModal } from "../site-selection-modal";
import { openSearchModal } from "../modals/search-modal";
import type HaloPlugin from "../main";

export async function searchPosts(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);
    }

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