"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var esbuild_utils_exports = {};
__export(esbuild_utils_exports, {
  BuildCodeContext: () => BuildCodeContext,
  BuildContext: () => BuildContext,
  BuildEntrypointContext: () => BuildEntrypointContext,
  buildCode: () => buildCode,
  buildEntrypoint: () => buildEntrypoint
});
module.exports = __toCommonJS(esbuild_utils_exports);
var esb = __toESM(require("esbuild"));
var import_lodash = __toESM(require("lodash"));
__reExport(esbuild_utils_exports, require("esbuild"), module.exports);
const DEFAULT_OPTIONS = {
  bundle: true,
  sourcemap: false,
  logLevel: "silent",
  platform: "node",
  target: "es2020",
  legalComments: "none",
  logOverride: { "equals-negative-zero": "silent" },
  keepNames: true,
  // important : https://github.com/node-fetch/node-fetch/issues/784#issuecomment-1014768204
  minify: false
};
class BuildContext {
  _context;
  _previousProps;
  _previousOpts = {};
  async rebuild(props, opts = {}) {
    if (!this._context || !import_lodash.default.isEqual(props, this._previousProps) || !import_lodash.default.isEqual(opts, this._previousOpts)) {
      if (this._context) {
        await this._context.dispose();
      }
      this._context = await this._createContext(props, opts);
      this._previousOpts = opts;
      this._previousProps = props;
    }
    return await this._context?.rebuild();
  }
}
class BuildCodeContext extends BuildContext {
  _createContext(props, opts = {}) {
    const { absWorkingDir, code, outfile } = props;
    return esb.context({
      ...DEFAULT_OPTIONS,
      ...opts,
      absWorkingDir,
      outfile,
      stdin: { contents: code, resolveDir: absWorkingDir, loader: "ts" },
      write: true
    });
  }
}
class BuildEntrypointContext extends BuildContext {
  _createContext(props, opts = {}) {
    const { absWorkingDir, entrypoint } = props;
    return esb.context({
      ...DEFAULT_OPTIONS,
      ...opts,
      absWorkingDir,
      entryPoints: [entrypoint],
      outfile: void 0,
      write: false
    });
  }
}
function buildCode(props, opts = {}) {
  const { absWorkingDir, code, outfile } = props;
  return esb.build({
    ...DEFAULT_OPTIONS,
    ...opts,
    absWorkingDir,
    outfile,
    stdin: { contents: code, resolveDir: absWorkingDir, loader: "ts" },
    write: true
  });
}
function buildEntrypoint(props, opts = {}) {
  const { absWorkingDir, entrypoint } = props;
  return esb.build({
    ...DEFAULT_OPTIONS,
    ...opts,
    absWorkingDir,
    entryPoints: [entrypoint],
    outfile: void 0,
    write: false
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BuildCodeContext,
  BuildContext,
  BuildEntrypointContext,
  buildCode,
  buildEntrypoint,
  ...require("esbuild")
});
//# sourceMappingURL=esbuild-utils.js.map
