import { createRequire } from 'module'; const require = createRequire(import.meta.url);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });

// src/install.ts
import { execFileSync } from "child_process";
import { chmodSync, createWriteStream, existsSync, mkdirSync, renameSync } from "fs";
import { dirname, join } from "path";
import { pipeline } from "stream/promises";
import { fileURLToPath } from "url";
import fetch from "node-fetch";

// src/constants.ts
var TL_VERSION = "0.24.8";
var RELEASES_BASE = `https://github.com/teal-language/tl/releases/download/v${TL_VERSION}`;
var PLATFORM_MAP = {
  "linux-x64": {
    asset: `tl-${TL_VERSION}-linux-x86_64.tar.gz`,
    exe: "tl"
  },
  "win32-x64": {
    asset: `tl-${TL_VERSION}-windows-x86_64.zip`,
    exe: "tl.exe"
  }
};

// src/install.ts
var __dirname = dirname(fileURLToPath(import.meta.url));
var BIN_DIR = join(__dirname, "..", "bin");
async function install() {
  const platformKey = `${process.platform}-${process.arch}`;
  const platformInfo = PLATFORM_MAP[platformKey];
  if (!platformInfo) {
    console.warn(`[@tlfx/compiler] No pre-built binary available for ${platformKey}.
  Set TL_BINARY=/path/to/tl in your environment to use your own.`);
    return;
  }
  const exePath = join(BIN_DIR, platformInfo.exe);
  if (existsSync(exePath)) {
    console.log("[@tlfx/compiler] Binary already present, skipping download.");
    return;
  }
  mkdirSync(BIN_DIR, {
    recursive: true
  });
  const url = `${RELEASES_BASE}/${platformInfo.asset}`;
  console.log(`[@tlfx/compiler] Downloading tl v${TL_VERSION} for ${platformKey}...`);
  const res = await fetch(url, {
    redirect: "follow"
  });
  if (!res.ok || !res.body) {
    throw new Error(`Download failed: ${res.status} ${res.statusText}`);
  }
  const archivePath = join(BIN_DIR, platformInfo.asset);
  await pipeline(res.body, createWriteStream(archivePath));
  const extractedDirName = platformInfo.asset.replace(/\.tar\.gz$/, "").replace(/\.zip$/, "");
  const extractedBinaryPath = join(BIN_DIR, extractedDirName, platformInfo.exe);
  if (platformInfo.asset.endsWith(".tar.gz")) {
    execFileSync("tar", [
      "-xzf",
      archivePath,
      "-C",
      BIN_DIR
    ]);
  } else if (platformInfo.asset.endsWith(".zip")) {
    execFileSync("powershell", [
      "-Command",
      `Expand-Archive -Path '${archivePath}' -DestinationPath '${BIN_DIR}' -Force`
    ]);
  }
  if (!existsSync(extractedBinaryPath)) {
    throw new Error(`[@tlfx/compiler] Expected binary at ${extractedBinaryPath} after extraction, but it wasn't found. The archive layout may have changed \u2014 please open an issue.`);
  }
  renameSync(extractedBinaryPath, exePath);
  if (process.platform !== "win32") {
    chmodSync(exePath, 493);
  }
  console.log(`[@tlfx/compiler] tl binary ready at ${exePath}`);
}
__name(install, "install");
install().catch((err) => {
  const message = err instanceof Error ? err.message : String(err);
  console.warn(`[@tlfx/compiler] Binary install failed: ${message}`);
  console.warn(`[@tlfx/compiler] Set TL_BINARY=/path/to/tl to use your own.`);
});
