"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 cache_utils_exports = {};
__export(cache_utils_exports, {
  FSKeyValueCache: () => FSKeyValueCache
});
module.exports = __toCommonJS(cache_utils_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var json = __toESM(require("./json-utils"));
class FSKeyValueCache {
  constructor(_filepath) {
    this._filepath = _filepath;
  }
  _initialized = false;
  async init() {
    if (this._initialized) {
      return;
    }
    const dirname = import_path.default.dirname(this._filepath);
    if (!import_fs.default.existsSync(dirname)) {
      await import_fs.default.promises.mkdir(dirname, { recursive: true });
    }
    if (!import_fs.default.existsSync(this._filepath)) {
      await this._writeJSON(this._filepath, {});
    }
    this._initialized = true;
  }
  async sync(key, value, prompt) {
    await this.init();
    if (value) {
      await this.set(key, value);
      return value;
    }
    const data = await this.get(key);
    const newValue = await prompt(data);
    await this.set(key, newValue);
    return newValue;
  }
  async has(key) {
    await this.init();
    const data = await this._readJSON(this._filepath);
    return data[key] !== void 0;
  }
  async get(key) {
    await this.init();
    const data = await this._readJSON(this._filepath);
    return data[key];
  }
  async set(key, value) {
    await this.init();
    const data = await this._readJSON(this._filepath);
    data[key] = value;
    return this._writeJSON(this._filepath, data);
  }
  async rm(key) {
    await this.init();
    const data = await this._readJSON(this._filepath);
    delete data[key];
    return this._writeJSON(this._filepath, data);
  }
  async clear() {
    await this.init();
    return this._writeJSON(this._filepath, {});
  }
  _writeJSON = (filepath, data) => {
    const fileContent = JSON.stringify(data, null, 2);
    return import_fs.default.promises.writeFile(filepath, fileContent);
  };
  _readJSON = async (filepath) => {
    const fileContent = await import_fs.default.promises.readFile(filepath, "utf8");
    const parseResult = json.safeParseJson(fileContent);
    if (!parseResult.success) {
      throw new Error(`Failed to parse JSON file at ${filepath}: ${parseResult.error.message}`);
    }
    return parseResult.data;
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FSKeyValueCache
});
//# sourceMappingURL=cache-utils.js.map
