#!/usr/bin/env node
/**
 * Re-sync the vendored @photo-gallery/sdk from the sibling SDK checkout.
 *
 * The SDK is not published to a registry yet, so it is vendored into `vendor/photo-gallery-sdk`
 * and depended on as `file:./vendor/photo-gallery-sdk` — a path INSIDE this repo, so CI and Vercel
 * (which only ever check out this repo) can resolve it. See vendor/README.md.
 *
 *     npm run sync:gallery-sdk
 *     git add vendor/photo-gallery-sdk && git commit -m "chore: sync @photo-gallery/sdk"
 *
 * Only what the package would publish is copied. Its `node_modules` is deliberately left behind:
 * without it the SDK's sources resolve `@types/react` from this repo (React 19) instead of the
 * pnpm workspace's React 18, which is what lets them type-check here.
 */

import { cp, readFile, rm, stat, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const source =
  process.env.GALLERY_SDK_PATH ??
  resolve(root, "../advance-photo-gallery-web-sdk/packages/photo-sdk");
const target = resolve(root, "vendor/photo-gallery-sdk");

const ENTRIES = ["src", "package.json", "README.md"];

const exists = async (p) => {
  try {
    await stat(p);
    return true;
  } catch {
    return false;
  }
};

if (!(await exists(source))) {
  console.error(`[sync:gallery-sdk] SDK checkout not found at ${source}`);
  console.error("  Clone advance-photo-gallery-web-sdk beside this repo, or set GALLERY_SDK_PATH.");
  process.exit(1);
}

for (const entry of ENTRIES) {
  const from = resolve(source, entry);
  if (!(await exists(from))) continue;
  const to = resolve(target, entry);
  await rm(to, { recursive: true, force: true });
  await cp(from, to, { recursive: true });
  console.log(`[sync:gallery-sdk] ${entry}`);
}

/*
 * Strip devDependencies + scripts from the vendored manifest.
 *
 * npm never installs a published package's devDependencies, but it DOES install them for a local
 * `file:` path package. Left in place, the SDK's `@types/react@18` lands in
 * vendor/photo-gallery-sdk/node_modules and its sources then type-check against React 18 inside a
 * React 19 program — "Type 'bigint' is not assignable to type 'ReactNode'". Removing them makes the
 * vendored copy behave exactly like the tarball it stands in for.
 */
const manifestPath = resolve(target, "package.json");
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
delete manifest.devDependencies;
delete manifest.scripts;
manifest._vendored = {
  from: "advance-photo-gallery-web-sdk/packages/photo-sdk",
  note: "Generated by scripts/sync-gallery-sdk.mjs — do not hand-edit. See vendor/README.md.",
};
await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
console.log("[sync:gallery-sdk] package.json (devDependencies + scripts stripped)");

console.log("[sync:gallery-sdk] done — commit vendor/photo-gallery-sdk, then restart the dev server.");
