"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 lock_manager_exports = {};
__export(lock_manager_exports, {
  LockManager: () => LockManager
});
module.exports = __toCommonJS(lock_manager_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
const KIROGRAPH_DIR = ".kirograph";
const LOCK_FILE = "kirograph.lock";
const DIRTY_FILE = "dirty";
const LOCK_STALE_MS = 5 * 60 * 1e3;
class LockManager {
  constructor(projectRoot) {
    this.projectRoot = projectRoot;
    const dir = path.join(projectRoot, KIROGRAPH_DIR);
    this.lockPath = path.join(dir, LOCK_FILE);
    this.dirtyPath = path.join(dir, DIRTY_FILE);
  }
  // ── Process lock ───────────────────────────────────────────────────────────
  acquire() {
    if (fs.existsSync(this.lockPath)) {
      try {
        const content = fs.readFileSync(this.lockPath, "utf8").trim();
        const [pidStr, tsStr] = content.split(":");
        const pid = parseInt(pidStr, 10);
        const ts = parseInt(tsStr, 10);
        if (!isNaN(pid) && pid !== process.pid) {
          const isStale = !isNaN(ts) && Date.now() - ts > LOCK_STALE_MS;
          if (!isStale) {
            try {
              process.kill(pid, 0);
              throw new Error(`KiroGraph is locked by PID ${pid}. Use 'kirograph unlock' to force-release.`);
            } catch (e) {
              if (e.message.includes("KiroGraph is locked")) throw e;
            }
          }
        }
      } catch (e) {
        if (e.message.includes("KiroGraph is locked")) throw e;
      }
    }
    fs.writeFileSync(this.lockPath, `${process.pid}:${Date.now()}`);
  }
  release() {
    try {
      fs.unlinkSync(this.lockPath);
    } catch {
    }
  }
  forceRelease() {
    this.release();
  }
  // ── Dirty marker ───────────────────────────────────────────────────────────
  markDirty() {
    fs.writeFileSync(this.dirtyPath, String(Date.now()));
  }
  clearDirty() {
    try {
      fs.unlinkSync(this.dirtyPath);
    } catch {
    }
  }
  isDirty() {
    return fs.existsSync(this.dirtyPath);
  }
  /** Returns true if a sync/index is currently running (lock file held by another process). */
  isLocked() {
    if (!fs.existsSync(this.lockPath)) return false;
    try {
      const content = fs.readFileSync(this.lockPath, "utf8").trim();
      const [pidStr, tsStr] = content.split(":");
      const pid = parseInt(pidStr, 10);
      const ts = parseInt(tsStr, 10);
      if (isNaN(pid) || pid === process.pid) return false;
      const isStale = !isNaN(ts) && Date.now() - ts > LOCK_STALE_MS;
      if (isStale) return false;
      try {
        process.kill(pid, 0);
        return true;
      } catch {
        return false;
      }
    } catch {
      return false;
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LockManager
});
//# sourceMappingURL=lock-manager.js.map
