// src/actions/package/clean-outputs.ts
import path2 from "node:path";
import chalk2 from "chalk";

// src/lib/crossSpawn.ts
import crossSpawn from "cross-spawn";
function spawnSync(command, args, options) {
  return crossSpawn.sync(command, args, options);
}

// src/lib/deepMerge.ts
var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
function isPlainObject(value) {
  if (typeof value !== "object" || value === null) return false;
  const proto = Object.getPrototypeOf(value);
  return proto === Object.prototype || proto === null;
}
function isMergeable(value) {
  return Array.isArray(value) || isPlainObject(value);
}
function cloneValue(value) {
  if (Array.isArray(value)) {
    return value.map((item) => cloneValue(item));
  }
  if (isPlainObject(value)) {
    const result = {};
    for (const [key, item] of Object.entries(value)) {
      if (UNSAFE_KEYS.has(key)) continue;
      result[key] = cloneValue(item);
    }
    return result;
  }
  return value;
}
function mergeObject(target, source, options) {
  const destination = {};
  if (isPlainObject(target)) {
    for (const [key, item] of Object.entries(target)) {
      if (UNSAFE_KEYS.has(key)) continue;
      destination[key] = cloneValue(item);
    }
  }
  for (const [key, sourceValue] of Object.entries(source)) {
    if (UNSAFE_KEYS.has(key)) continue;
    if (Object.hasOwn(target, key) && isMergeable(sourceValue)) {
      const customMerge = options.customMerge?.(key);
      destination[key] = (customMerge ?? deepMerge)(target[key], sourceValue, options);
    } else {
      destination[key] = cloneValue(sourceValue);
    }
  }
  return destination;
}
function deepMerge(target, source, options = {}) {
  const isSourceIsArray = Array.isArray(source);
  const isTargetIsArray = Array.isArray(target);
  if (isSourceIsArray !== isTargetIsArray) {
    return cloneValue(source);
  }
  if (isSourceIsArray) {
    return [...target, ...source].map((item) => cloneValue(item));
  }
  return mergeObject(target, source, options);
}

// src/lib/deleteGlob.ts
import fs from "node:fs";
import { glob } from "glob";
var deleteGlob = (globPath) => {
  const files = glob.sync(globPath);
  for (const file of files) {
    fs.rmSync(file, { recursive: true, force: true });
  }
};

// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/loadConfig.ts
import chalk from "chalk";
import { cosmiconfig } from "cosmiconfig";
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
var configState = {
  config: void 0,
  rootConfigPath: void 0
};
function createExplorer() {
  return cosmiconfig("xy", { cache: true, loaders: { ".ts": TypeScriptLoader() } });
}
var loadConfig = async (params) => {
  if (configState.config === void 0) {
    const cosmicConfigResult = await createExplorer().search();
    configState.config = cosmicConfigResult?.config ?? {};
    configState.rootConfigPath = cosmicConfigResult?.filepath;
    const configFilePath = cosmicConfigResult?.filepath;
    if (configFilePath !== void 0) {
      console.log(chalk.green(`Loaded config from ${configFilePath}`));
      if (Boolean(configState.config.verbose)) {
        console.log(chalk.gray(JSON.stringify(configState.config, null, 2)));
      }
    }
  }
  return deepMerge(configState.config, params ?? {});
};

// src/lib/packageName.ts
import { readFileSync } from "node:fs";
import path from "node:path";
function packageName() {
  if (process.env.npm_package_name != null && process.env.npm_package_name.length > 0) {
    return process.env.npm_package_name;
  }
  try {
    const pkgPath = path.join(INIT_CWD(), "package.json");
    const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
    return pkg.name;
  } catch {
    return void 0;
  }
}

// src/actions/package/clean-outputs.ts
var packageCleanOutputs = () => {
  const pkg = INIT_CWD();
  const pkgName = packageName();
  const folders = [path2.join(pkg, "dist"), path2.join(pkg, "build"), path2.join(pkg, "docs")];
  console.log(chalk2.green(`Cleaning Outputs [${pkgName}]`));
  for (const folder of folders) {
    deleteGlob(folder);
  }
  return 0;
};

// src/actions/package/clean-typescript.ts
import path3 from "node:path";
import chalk3 from "chalk";
var packageCleanTypescript = () => {
  const pkg = INIT_CWD();
  const pkgName = packageName();
  console.log(chalk3.green(`Cleaning Typescript [${pkgName}]`));
  const files = [path3.join(pkg, "*.tsbuildinfo"), path3.join(pkg, ".tsconfig.*"), path3.join(pkg, ".eslintcache")];
  for (const file of files) {
    deleteGlob(file);
  }
  return 0;
};

// src/actions/package/clean.ts
var packageClean = () => {
  return [packageCleanOutputs(), packageCleanTypescript()].reduce((prev, value) => prev + value, 0);
};

// src/actions/package/compile/compile.ts
import { cwd as cwd4 } from "node:process";
import chalk9 from "chalk";

// src/actions/package/compile/packageCompileEsbuild.ts
import fs3 from "node:fs";
import path6 from "node:path";
import { cwd as cwd3 } from "node:process";
import chalk8 from "chalk";
import { build } from "esbuild";

// src/actions/package/compile/inputs.ts
import chalk4 from "chalk";
import { glob as glob2 } from "glob";
var getAllInputs = (srcDir, isVerbose = false) => {
  return glob2.sync(`${srcDir}/**/*.{ts,tsx,mts,cts}`, { posix: true }).map((file) => {
    const result = file.slice(Math.max(0, srcDir.length + 1));
    if (isVerbose) {
      console.log(chalk4.gray(`getAllInputs: ${JSON.stringify(result, null, 2)}`));
    }
    return result;
  });
};

// src/actions/package/compile/buildEntries.ts
var bareSpecOrStoryRe = /(?:^|\/)(?:spec|stories)\.(?:ts|tsx|mts|cts)$/;
var isSpecOrStory = (entry) => {
  return entry.includes(".spec.") || entry.includes(".stories.") || entry.startsWith("spec/") || entry.includes("/spec/") || entry.startsWith("stories/") || entry.includes("/stories/") || bareSpecOrStoryRe.test(entry);
};
var isDeclaration = (entry) => /\.d\.(?:ts|mts|cts)$/.test(entry);
var buildEntries = (srcDir, entryMode = "single", options, shouldExcludeSpecAndStories = true, isVerbose = false) => {
  let entries;
  switch (entryMode) {
    case "platform": {
      entries = ["index-node.ts", "index-browser.ts"];
      break;
    }
    case "all": {
      entries = (shouldExcludeSpecAndStories ? getAllInputs(srcDir).filter((entry) => !isSpecOrStory(entry)) : getAllInputs(srcDir)).filter((entry) => !isDeclaration(entry));
      break;
    }
    case "custom": {
      entries = [];
      break;
    }
    default: {
      entries = ["index.ts"];
      break;
    }
  }
  if (typeof options !== "boolean" && Array.isArray(options?.entry)) {
    entries.push(...options.entry);
  }
  if (isVerbose) console.log(`buildEntries [${entryMode}] ${entries.length}`);
  return entries;
};

// src/actions/package/compile/deepMerge.ts
function deepMerge2(target, source) {
  for (const [key, sourceVal] of Object.entries(source)) {
    if (typeof sourceVal === "object" && sourceVal !== null && !Array.isArray(sourceVal)) {
      if (!Object.hasOwn(target, key) || typeof target[key] !== "object") {
        target[key] = {};
      }
      deepMerge2(target[key], sourceVal);
    } else {
      target[key] = sourceVal;
    }
  }
  return target;
}
function deepMergeObjects(objects) {
  const result = {};
  for (const obj of objects) {
    deepMerge2(result, obj);
  }
  return result;
}

// src/actions/package/compile/entryOutputName.ts
import path4 from "node:path";
var platformSourceSegments = /* @__PURE__ */ new Set(["browser", "neutral", "node"]);
var toPosixPath = (value) => value.split(path4.sep).join("/");
var normalizeEntryPath = (entry) => toPosixPath(entry).replace(/^\.?\//u, "");
var customEntryOutputName = (entry) => {
  const parsed = path4.posix.parse(normalizeEntryPath(entry));
  const withoutExtension = path4.posix.join(parsed.dir, parsed.name);
  const segments = withoutExtension.split("/");
  return segments.length > 1 && platformSourceSegments.has(segments[0]) ? segments.slice(1).join("/") : withoutExtension;
};

// src/actions/package/compile/resolveCompilePlatforms.ts
import { cwd } from "node:process";
import chalk5 from "chalk";
var resolvePlatformRecord = (value) => {
  if (value === void 0 || value === true) return { src: {} };
  if (value === false) return {};
  const { esbuildOptions: _legacyEsbuildOptions, ...entries } = value;
  return entries;
};
var classifyPlatform = (value) => {
  if (value === void 0) {
    return {
      deprecatedEmptyObject: false,
      entries: {},
      state: "unset"
    };
  }
  if (typeof value === "boolean") {
    return {
      deprecatedEmptyObject: false,
      entries: value ? { src: {} } : {},
      state: value ? "build" : "off"
    };
  }
  const isDeprecatedEmptyObject = Object.keys(value).length === 0;
  const entries = resolvePlatformRecord(value);
  return {
    deprecatedEmptyObject: isDeprecatedEmptyObject,
    entries,
    state: Object.keys(entries).length > 0 ? "build" : "off"
  };
};
var resolveCompilePlatforms = (compile) => {
  const node = classifyPlatform(compile?.node);
  const browser = classifyPlatform(compile?.browser);
  const neutral = classifyPlatform(compile?.neutral);
  const isAnyBuild = node.state === "build" || browser.state === "build" || neutral.state === "build";
  const neutralEntries = neutral.state === "build" ? neutral.entries : !isAnyBuild && neutral.state === "unset" ? { src: {} } : {};
  const deprecatedEmptyObjectTargets = [
    ...node.deprecatedEmptyObject ? ["node"] : [],
    ...browser.deprecatedEmptyObject ? ["browser"] : [],
    ...neutral.deprecatedEmptyObject ? ["neutral"] : []
  ];
  return {
    browser: browser.state === "build" ? browser.entries : {},
    deprecatedEmptyObjectTargets,
    neutral: neutralEntries,
    node: node.state === "build" ? node.entries : {}
  };
};
var deprecatedEmptyObjectWarned = /* @__PURE__ */ new Set();
var warnDeprecatedEmptyObjectTargets = (targets) => {
  for (const target of targets) {
    const key = `${cwd()}:${target}`;
    if (deprecatedEmptyObjectWarned.has(key)) continue;
    deprecatedEmptyObjectWarned.add(key);
    console.warn(chalk5.yellow(
      `[xy] Deprecated: compile.${target} = {} is treated as "do not build". Set it to \`false\` or remove the field.`
    ));
  }
};

// src/actions/package/compile/runTsc.ts
import { cwd as cwd2 } from "node:process";
import chalk6 from "chalk";
var runTsc = (configPath, isVerbose = false, extraArgs = []) => {
  const args = ["-p", configPath, ...extraArgs];
  if (isVerbose) {
    console.log(chalk6.gray(`tsc ${args.join(" ")}`));
  }
  const result = spawnSync("tsc", args, {
    cwd: cwd2(),
    stdio: "inherit"
  });
  return result.status ?? (result.error === void 0 ? 0 : 1);
};

// src/actions/package/compile/validateFullPackage.ts
import fs2 from "node:fs";
import path5 from "node:path";
import chalk7 from "chalk";
import ts, { formatDiagnosticsWithColorAndContext, sys } from "typescript";
var toPosixPath2 = (value) => value.split(path5.sep).join("/");
var relativePath = (from, to) => {
  const result = toPosixPath2(path5.relative(from, to));
  return result.startsWith(".") ? result : `./${result}`;
};
var formatTsConfigDiagnostics = (diagnostics, pkg) => {
  return formatDiagnosticsWithColorAndContext(diagnostics, {
    getCanonicalFileName: (fileName) => fileName,
    getCurrentDirectory: () => pkg,
    getNewLine: () => sys.newLine
  });
};
var isInPackageSubdirectory = (pkg, fileName, directoryName) => {
  const relative = toPosixPath2(path5.relative(path5.resolve(pkg, directoryName), fileName));
  return relative !== "" && !relative.startsWith("../") && relative !== "..";
};
var createFullValidationConfig = (pkg, tsconfigPath) => {
  const configFile = ts.readConfigFile(tsconfigPath, (fileName) => ts.sys.readFile(fileName));
  if (configFile.error) {
    return { configPath: tsconfigPath, diagnostics: [configFile.error] };
  }
  const parsed = ts.parseJsonConfigFileContent(
    configFile.config,
    ts.sys,
    pkg,
    void 0,
    tsconfigPath
  );
  const buildDir = path5.resolve(pkg, "build");
  fs2.mkdirSync(buildDir, { recursive: true });
  const configPath = path5.resolve(buildDir, "tsconfig.package-validate.json");
  const files = parsed.fileNames.filter((fileName) => !isInPackageSubdirectory(pkg, fileName, "packages")).map((fileName) => relativePath(buildDir, fileName));
  if (files.length === 0) {
    const emptyFile = path5.resolve(buildDir, "tsconfig.package-validate-empty.d.ts");
    fs2.writeFileSync(emptyFile, "");
    files.push(relativePath(buildDir, emptyFile));
  }
  fs2.writeFileSync(
    configPath,
    `${JSON.stringify({
      compilerOptions: {
        noEmit: true,
        rootDir: relativePath(buildDir, pkg)
      },
      extends: relativePath(buildDir, tsconfigPath),
      files,
      include: []
    }, null, 2)}
`
  );
  return { configPath, diagnostics: parsed.errors };
};
var validateFullPackage = (pkg, isVerbose = false) => {
  const tsconfigPath = path5.resolve(pkg, "tsconfig.json");
  if (!fs2.existsSync(tsconfigPath)) {
    console.warn(chalk7.yellow("Compile:Validate skipped \u2014 tsconfig.json not found"));
    return 0;
  }
  if (isVerbose) {
    console.log(chalk7.cyan("Compile:Validate [tsconfig.json]"));
  }
  const { configPath, diagnostics } = createFullValidationConfig(pkg, tsconfigPath);
  if (diagnostics.length > 0) {
    console.error(formatTsConfigDiagnostics(diagnostics, pkg));
    return 1;
  }
  const result = runTsc(configPath, isVerbose);
  if (result !== 0) {
    console.error(chalk7.red(`Compile:Validate failed (tsc exit code ${result})`));
  }
  return result;
};

// src/actions/package/compile/packageCompileEsbuild.ts
var isBuildFormat = (value) => {
  return typeof value === "string" && ["cjs", "esm", "iife"].includes(value);
};
var legacyIgnoredOptions = /* @__PURE__ */ new Set([
  "clean",
  "cjsInterop",
  "dts",
  "entry",
  "outDir",
  "silent",
  "skipNodeModulesBundle"
]);
var supportedEsbuildOptions = /* @__PURE__ */ new Set([
  "absWorkingDir",
  "alias",
  "allowOverwrite",
  "assetNames",
  "banner",
  "bundle",
  "charset",
  "chunkNames",
  "color",
  "conditions",
  "define",
  "drop",
  "dropLabels",
  "entryNames",
  "entryPoints",
  "external",
  "footer",
  "format",
  "globalName",
  "ignoreAnnotations",
  "inject",
  "jsx",
  "jsxDev",
  "jsxFactory",
  "jsxFragment",
  "jsxImportSource",
  "jsxSideEffects",
  "keepNames",
  "legalComments",
  "lineLimit",
  "loader",
  "logLevel",
  "logLimit",
  "logOverride",
  "mainFields",
  "mangleCache",
  "mangleProps",
  "mangleQuoted",
  "metafile",
  "minify",
  "minifyIdentifiers",
  "minifySyntax",
  "minifyWhitespace",
  "nodePaths",
  "outExtension",
  "outbase",
  "outdir",
  "outfile",
  "packages",
  "platform",
  "plugins",
  "preserveSymlinks",
  "publicPath",
  "pure",
  "reserveProps",
  "resolveExtensions",
  "sourcemap",
  "sourceRoot",
  "sourcesContent",
  "splitting",
  "stdin",
  "supported",
  "target",
  "treeShaking",
  "tsconfig",
  "tsconfigRaw",
  "watch",
  "write"
]);
var relativePath2 = (from, to) => {
  const result = toPosixPath(path6.relative(from, to));
  return result.startsWith(".") ? result : `./${result}`;
};
var safeFilePart = (value) => value.replaceAll(/[^\dA-Za-z-]+/g, "-").replaceAll(/(?:^-|-$)/g, "") || "src";
var createEsbuildEntryPoints = (srcDir, entries, entryMode = "single") => {
  if (entryMode !== "custom") {
    return entries.map((entry) => `${srcDir}/${normalizeEntryPath(entry)}`);
  }
  const entryPoints = {};
  for (const entry of entries) {
    const outputName = customEntryOutputName(entry);
    if (entryPoints[outputName] !== void 0) {
      throw new Error(`Duplicate custom entry output "${outputName}" from "${entry}"`);
    }
    entryPoints[outputName] = `${srcDir}/${normalizeEntryPath(entry)}`;
  }
  return entryPoints;
};
var createTypescriptConfig = ({
  hasDeclaration = false,
  outDir,
  pkg,
  srcDir
}) => {
  const buildDir = path6.resolve(pkg, "build");
  const sourceRoot = path6.resolve(pkg, srcDir);
  return {
    compilerOptions: {
      ...hasDeclaration ? {
        declaration: true,
        declarationMap: true,
        emitDeclarationOnly: true,
        noEmit: false,
        outDir: outDir === void 0 ? void 0 : relativePath2(buildDir, path6.resolve(pkg, outDir))
      } : { noEmit: true },
      rootDir: relativePath2(buildDir, sourceRoot)
    },
    exclude: [
      `${relativePath2(buildDir, sourceRoot)}/**/*.spec.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/*.stories.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/*.example.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/spec/**`,
      `${relativePath2(buildDir, sourceRoot)}/**/stories/**`
    ],
    extends: relativePath2(buildDir, path6.resolve(pkg, "tsconfig.json")),
    include: [`${relativePath2(buildDir, sourceRoot)}/**/*`]
  };
};
var writeTypescriptConfig = (pkg, platform, srcDir, hasDeclaration, outDir) => {
  const buildDir = path6.resolve(pkg, "build");
  fs3.mkdirSync(buildDir, { recursive: true });
  const configPath = path6.resolve(buildDir, `tsconfig.package-${hasDeclaration ? "dts" : "check"}-${platform}-${safeFilePart(srcDir)}.json`);
  fs3.writeFileSync(
    configPath,
    `${JSON.stringify(createTypescriptConfig({
      hasDeclaration,
      outDir,
      pkg,
      srcDir
    }), null, 2)}
`
  );
  return configPath;
};
function copyDeclarationFiles(fromDir, toDir) {
  if (path6.resolve(fromDir) === path6.resolve(toDir) || !fs3.existsSync(fromDir)) return;
  const copyFrom = (dir) => {
    const entries = fs3.readdirSync(dir, { withFileTypes: true });
    for (const entry of entries) {
      const sourcePath = path6.join(dir, entry.name);
      const relative = path6.relative(fromDir, sourcePath);
      const targetPath = path6.join(toDir, relative);
      if (entry.isDirectory()) {
        copyFrom(sourcePath);
      } else if (entry.isFile() && (entry.name.endsWith(".d.ts") || entry.name.endsWith(".d.ts.map"))) {
        fs3.mkdirSync(path6.dirname(targetPath), { recursive: true });
        fs3.copyFileSync(sourcePath, targetPath);
      }
    }
  };
  copyFrom(fromDir);
}
var normalizeOptions = (options = {}, isVerbose = false) => {
  const normalized = {};
  const ignored = [];
  for (const [key, value] of Object.entries(options)) {
    if (legacyIgnoredOptions.has(key)) {
      ignored.push(key);
      continue;
    }
    if (key === "format" && Array.isArray(value)) {
      if (isBuildFormat(value[0])) {
        normalized.format = value[0];
      }
      continue;
    }
    if (key === "outExtension" && typeof value === "function") {
      ignored.push(key);
      continue;
    }
    if (supportedEsbuildOptions.has(key)) {
      normalized[key] = value;
      continue;
    }
    ignored.push(key);
  }
  if (isVerbose && ignored.length > 0) {
    console.warn(chalk8.yellow(`[xy] Ignoring unsupported compile options for esbuild: ${[...new Set(ignored)].toSorted((a, b) => a.localeCompare(b)).join(", ")}`));
  }
  return normalized;
};
var esbuildOptions = (options = [], isVerbose = false) => {
  const standardLoaders = {
    ".gif": "copy",
    ".html": "copy",
    ".jpg": "copy",
    ".json": "json",
    ".png": "copy",
    ".svg": "text",
    ".webp": "copy"
  };
  const standardOptions = {
    bundle: true,
    format: "esm",
    loader: standardLoaders,
    outExtension: { ".js": ".mjs" },
    packages: "external",
    sourcemap: true,
    target: "esnext"
  };
  return deepMergeObjects([standardOptions, ...options.map((option) => normalizeOptions(option, isVerbose))]);
};
var resolvePlatformEsbuildOptions = (value) => {
  if (typeof value !== "object" || value === null) return {};
  const entry = value.esbuildOptions;
  return entry ?? {};
};
var getRootOutDir = (options) => {
  return options.outDir ?? options.outdir ?? "dist";
};
var compileFolder = async (srcDir, entries, options, platform, outDir, shouldCheckTypes, emitDeclarations, entryMode, isVerbose) => {
  const pkg = cwd3();
  if (isVerbose ?? false) {
    console.log(chalk8.gray(`compileFolder [${srcDir}, ${outDir}]`));
  }
  if (!fs3.existsSync(path6.resolve(pkg, srcDir))) {
    if (isVerbose ?? false) {
      console.warn(chalk8.yellow(`No source directory found in ${srcDir} to compile`));
    }
    return 0;
  }
  if (entries.length === 0) {
    console.warn(chalk8.yellow(`No entries found in ${srcDir} to compile`));
    return 0;
  }
  if (shouldCheckTypes) {
    const checkConfig = writeTypescriptConfig(pkg, platform, srcDir, false);
    const checkResult = runTsc(checkConfig, isVerbose);
    if (checkResult !== 0) {
      console.error(chalk8.red(`Compile:Validation had ${checkResult} errors`));
      return checkResult;
    }
  }
  const declarationResult = await emitDeclarations();
  if (declarationResult !== 0) {
    console.error(chalk8.red(`Compile:Declaration emit had ${declarationResult} errors`));
    return declarationResult;
  }
  const buildOptions = {
    ...options,
    entryPoints: createEsbuildEntryPoints(srcDir, entries, entryMode),
    outbase: srcDir,
    outdir: outDir,
    platform,
    tsconfig: "tsconfig.json"
  };
  if (isVerbose ?? false) {
    console.log(chalk8.cyan(`ESBUILD:build:start [${srcDir}]`));
    const pluginNames = buildOptions.plugins?.map((plugin) => plugin.name);
    const loggedOptions = { ...buildOptions, plugins: pluginNames };
    console.log(chalk8.gray(`ESBUILD:build:options [${JSON.stringify(loggedOptions, null, 2)}]`));
  }
  await build(buildOptions);
  if (isVerbose ?? false) {
    console.log(chalk8.cyan(`ESBUILD:build:stop [${srcDir}]`));
  }
  return 0;
};
function createDeclarationEmitter(isVerbose = false) {
  const declarationPromises = /* @__PURE__ */ new Map();
  return async (srcDir, rootOutDir, platform, outDir) => {
    const key = `${srcDir}\0${rootOutDir}`;
    let promise = declarationPromises.get(key);
    if (!promise) {
      promise = (async () => {
        const dtsConfig = writeTypescriptConfig(cwd3(), platform, srcDir, true, outDir);
        return { outDir, result: runTsc(dtsConfig, isVerbose) };
      })();
      declarationPromises.set(key, promise);
    }
    const emitted = await promise;
    if (emitted.result !== 0) return emitted.result;
    copyDeclarationFiles(emitted.outDir, outDir);
    return 0;
  };
}
function resolveCompileMode(config, mode) {
  const compile = config?.compile;
  const isVerbose = config?.verbose ?? false;
  const shouldEmit = !(mode.validateOnly ?? false);
  const shouldRunFullValidation = !(mode.emitOnly ?? false) && (compile?.validate ?? true);
  return {
    compile,
    shouldEmit,
    shouldRunFolderChecks: !shouldRunFullValidation && !(mode.emitOnly ?? false),
    shouldRunFullValidation,
    isVerbose
  };
}
async function compilePlatformEntries({
  shouldCheckTypes,
  compile,
  emitDeclarations,
  entries,
  legacyTsupOptions,
  platform,
  platformEsbuild,
  isVerbose
}) {
  const results = await Promise.all(
    Object.entries(entries).map(async ([srcDir, options]) => {
      const rootOutDir = getRootOutDir(options);
      const outDir = `${rootOutDir}/${platform}`;
      const entry = buildEntries(srcDir, compile?.entryMode, options, true, isVerbose);
      return compileFolder(
        srcDir,
        entry,
        esbuildOptions([
          platformEsbuild,
          legacyTsupOptions ?? {},
          compile?.esbuild?.options ?? {},
          options,
          { outdir: outDir }
        ], isVerbose),
        platform,
        outDir,
        shouldCheckTypes,
        () => emitDeclarations(srcDir, rootOutDir, platform, outDir),
        compile?.entryMode,
        isVerbose
      );
    })
  );
  return results.reduce((prev, value) => prev + value, 0);
}
var packageCompileEsbuild = async (config, mode = {}) => {
  const {
    compile,
    shouldEmit,
    shouldRunFolderChecks,
    shouldRunFullValidation,
    isVerbose
  } = resolveCompileMode(config, mode);
  if (isVerbose) {
    console.log("Compiling with esbuild and tsc");
  }
  if (shouldRunFullValidation) {
    const validateResult = validateFullPackage(cwd3(), isVerbose);
    if (validateResult !== 0) {
      return validateResult;
    }
  }
  if (!shouldEmit) return 0;
  const platforms = resolveCompilePlatforms(compile);
  warnDeprecatedEmptyObjectTargets(platforms.deprecatedEmptyObjectTargets);
  const compileForNode = platforms.node;
  const compileForBrowser = platforms.browser;
  const compileForNeutral = platforms.neutral;
  const nodeEsbuild = resolvePlatformEsbuildOptions(compile?.node);
  const browserEsbuild = resolvePlatformEsbuildOptions(compile?.browser);
  const neutralEsbuild = resolvePlatformEsbuildOptions(compile?.neutral);
  const legacyTsupOptions = compile?.tsup?.options;
  const emitDeclarations = createDeclarationEmitter(isVerbose);
  const [nodeErrors, browserErrors, neutralErrors] = await Promise.all([
    compilePlatformEntries({
      shouldCheckTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForNode,
      legacyTsupOptions,
      platform: "node",
      platformEsbuild: nodeEsbuild,
      isVerbose
    }),
    compilePlatformEntries({
      shouldCheckTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForBrowser,
      legacyTsupOptions,
      platform: "browser",
      platformEsbuild: browserEsbuild,
      isVerbose
    }),
    compilePlatformEntries({
      shouldCheckTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForNeutral,
      legacyTsupOptions,
      platform: "neutral",
      platformEsbuild: neutralEsbuild,
      isVerbose
    })
  ]);
  return nodeErrors + browserErrors + neutralErrors;
};

// src/actions/package/compile/compile.ts
var packageCompile = async (inConfig = {}, mode = {}) => {
  const pkg = cwd4();
  console.log(chalk9.green(`Compiling ${pkg}`));
  const config = await loadConfig(inConfig);
  return await packageCompileEsbuild(config, mode);
};

// src/actions/package/recompile.ts
var packageRecompile = async (config = {}) => {
  return packageClean() || await packageCompile(config);
};
export {
  packageRecompile
};
//# sourceMappingURL=recompile.mjs.map
