"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 __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 pkgjson_utils_exports = {};
__export(pkgjson_utils_exports, {
  findDependency: () => findDependency,
  readPackageJson: () => readPackageJson,
  safeReadPackageJson: () => safeReadPackageJson,
  writePackageJson: () => writePackageJson
});
module.exports = __toCommonJS(pkgjson_utils_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var json = __toESM(require("./json-utils"));
const FILE_NAME = "package.json";
const readPackageJson = async (path) => {
  const filePath = _resolveFilePath(path);
  if (!import_fs.default.existsSync(filePath)) {
    return void 0;
  }
  const strContent = await import_fs.default.promises.readFile(filePath, "utf8");
  const parseResult = json.safeParseJson(strContent);
  if (!parseResult.success) {
    throw new Error(`Failed to parse JSON at ${filePath}: ${parseResult.error.message}`);
  }
  return parseResult.data;
};
const safeReadPackageJson = async (path) => {
  try {
    const pkgJson = await readPackageJson(path);
    if (!pkgJson) {
      return { success: true };
    }
    return { success: true, pkgJson };
  } catch (thrown) {
    const error = thrown instanceof Error ? thrown : new Error(String(thrown));
    return { success: false, error };
  }
};
const findDependency = (pkgJson, name) => {
  const { dependencies, devDependencies, peerDependencies } = pkgJson;
  const allDependencies = { ...dependencies ?? {}, ...devDependencies ?? {}, ...peerDependencies ?? {} };
  return allDependencies[name];
};
const writePackageJson = async (path, pkgJson) => {
  const filePath = _resolveFilePath(path);
  await import_fs.default.promises.writeFile(filePath, JSON.stringify(pkgJson, null, 2));
};
function _resolveFilePath(path) {
  return import_path.default.basename(path) === FILE_NAME ? path : import_path.default.join(path, FILE_NAME);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findDependency,
  readPackageJson,
  safeReadPackageJson,
  writePackageJson
});
//# sourceMappingURL=pkgjson-utils.js.map
