import { defineConfig } from "zotero-plugin-scaffold";
import pkg from "./package.json";
import path from "path";

const ISOGIT_ESM = path.resolve(
  __dirname,
  "node_modules",
  "isomorphic-git",
  "index.js",
);

export default defineConfig({
  source: ["src", "addon"],
  dist: ".scaffold/build",
  name: pkg.config.addonName,
  id: pkg.config.addonID,
  namespace: pkg.config.addonRef,
  updateURL: `https://github.com/{{owner}}/{{repo}}/releases/download/release/${
    pkg.version.includes("-") ? "update-beta.json" : "update.json"
  }`,
  xpiDownloadLink:
    "https://github.com/{{owner}}/{{repo}}/releases/download/{{version}}/{{xpiName}}.xpi",

  release: {
    bumpp: {
      tag: "%s",
      commit: "chore(publish): release %s",
    },
  },

  build: {
    assets: ["addon/**/*.*"],
    define: {
      ...pkg.config,
      author: pkg.author,
      description: pkg.description,
      homepage: pkg.homepage,
      buildVersion: pkg.version,
      buildTime: "{{buildTime}}",
    },
    prefs: {
      prefix: pkg.config.prefsPrefix,
    },
    esbuildOptions: [
      {
        entryPoints: ["src/index.ts"],
        define: {
          __env__: `"${process.env.NODE_ENV}"`,
        },
        bundle: true,
        target: "firefox128",
        outfile: `.scaffold/build/addon/content/scripts/${pkg.config.addonRef}.js`,
        plugins: [
          {
            name: "isomorphic-git-esm",
            setup(build) {
              build.onResolve({ filter: /^isomorphic-git$/ }, () => ({
                path: ISOGIT_ESM,
              }));
            },
          },
        ],
      },
    ],
  },

  test: {
    waitForPlugin: `() => Zotero.${pkg.config.addonInstance}.data.initialized`,
  },

  // If you need to see a more detailed log, uncomment the following line:
  // logLevel: "trace",
});
