import { ContentScriptId } from "./types"

const __DEV__ = process.env.NODE_ENV == "development"

/** Manually register in the service worker */
export const contentMainScript = {
  id: ContentScriptId.main,
  js: ["js/content-main.js"],
  runAt: "document_start",
  world: "MAIN",
  matches: ["<all_urls>"],
} satisfies chrome.scripting.RegisteredContentScript

export const allFrameScript = {
  id: ContentScriptId.frame,
  js: ["js/content-frame.js"],
  allFrames: true,
  runAt: "document_start",
  matches: ["<all_urls>"],
} satisfies chrome.scripting.RegisteredContentScript

export const defaultSidebarPath = "sidebar.html"

const manifest = {
  // maximum of 45 characters
  name: "__MSG_name__",
  version: "1.2.11",
  // edge 12 characters
  // short_name: "__MSG_short_name__",
  // no more than 132 characters
  description: "__MSG_description__",
  manifest_version: 3,
  action: {
    default_icon: {
      16: "logo.png",
      24: "logo.png",
      32: "logo.png",
    },
    default_title: "__MSG_short_name__",
    default_popup: "popup.html",
  },
  default_locale: "en",
  icons: {
    16: "logo.png",
    32: "logo.png",
    48: "logo.png",
    128: "logo.png",
  },
  author: { email: "support@ziziyi.com" },
  background: {
    service_worker: "./src/bg/index.ts",
    type: "module" as const,
  },
  content_scripts: [
    // ── Original placeholder content script ──
    {
      matches: ["http://placeholder.ziziyi.com/*"],
      js: ["src/content/index.ts"],
      run_at: "document_start",
    },
    // ── SuperAssistant: Auto-inject into AI chat platforms ──
    {
      matches: [
        "https://chatgpt.com/*",
        "https://chat.openai.com/*",
        "https://gemini.google.com/*",
        "https://grok.com/*",
        "https://chat.deepseek.com/*",
        "https://www.perplexity.ai/*",
        "https://openrouter.ai/*",
        "https://t3.chat/*",
        "https://chat.mistral.ai/*",
      ],
      js: ["src/superassistant/index.ts"],
      run_at: "document_idle",
    },
  ],
  options_page: __DEV__ ? "sidebar.html" : undefined,
  side_panel: {
    default_path: defaultSidebarPath,
  },
  permissions: [
    "tabs",
    "scripting",
    "activeTab",
    "storage",
    "offscreen",
    "sidePanel",
    "declarativeNetRequestWithHostAccess",
    "declarativeNetRequestFeedback",
    "contextMenus",
    // "cookies",
  ],
  optional_permissions: [],
  host_permissions: ["<all_urls>"],
  minimum_chrome_version: "111",
  commands: {
    toggleMinimize: {
      suggested_key: {
        default: "Ctrl+Shift+1",
      },
      description: "__MSG_toggle_minimize_desc__",
      global: true,
    },
    toggleSidebar: {
      suggested_key: {
        default: "Ctrl+0",
      },
      description: "Toggle Sidebar",
    },
  },
  web_accessible_resources: [
    {
      resources: ["/js/*", "/assets/*", "/img/*", "sidebar.html", "logo.svg"],
      matches: ["<all_urls>"],
      use_dynamic_url: true,
    },
  ],
  content_security_policy: {
    extension_pages: __DEV__
      ? `script-src 'self' http://localhost:5173 'wasm-unsafe-eval';`
      : `script-src 'self' 'wasm-unsafe-eval'`,
  },
} satisfies Manifest as any

export default manifest
