import browserCompatData from "@mdn/browser-compat-data" with { type: "json" };
import { generateModuleSupport } from "./build-modules-support.mjs";
import {
  generateData,
  environments,
  writeFile,
  maybeDefineLegacyPluginAliases,
} from "./utils-build-data.mjs";
import pluginFeatures from "./data/plugin-features.mjs";

const compatData = browserCompatData.javascript;
if (process.cwd().endsWith("scripts")) {
  throw new Error("Please run this script from the root of the package");
}

function buildPluginFeatures() {
  // We ignore 'overlapping' here, because it's already generated by
  // built-bugfixes-targets.js which has a complete view over all the
  // plugins that we have data for.
  let { data: newData } = generateData(environments, pluginFeatures);
  // add export-namespace-from from @mdn/browser-compat-data
  newData["transform-export-namespace-from"] = generateModuleSupport(
    compatData.statements.export.namespace,
    true
  );

  // Add proposal-* aliases for backward compatibility.
  newData = maybeDefineLegacyPluginAliases(newData);
  const dataURL = new URL(`../data/plugins.json`, import.meta.url);

  if (!writeFile(newData, dataURL, "plugin")) {
    throw new Error(`Cannot update ${dataURL}`);
  }
}

buildPluginFeatures();
