import "./chunk-PZ5AY32C.js";

// src/session-toggles.ts
import { mkdir, readFile, writeFile } from "fs/promises";
import path from "path";
function encodeToggleKey(sessionKey, agentId) {
  return `${encodeURIComponent(sessionKey)}::${encodeURIComponent(agentId)}`;
}
function decodeToggleKey(key) {
  const [encodedSessionKey, encodedAgentId] = key.split("::");
  if (!encodedSessionKey || !encodedAgentId) return null;
  return {
    sessionKey: decodeURIComponent(encodedSessionKey),
    agentId: decodeURIComponent(encodedAgentId)
  };
}
async function safeReadToggleFile(filePath) {
  try {
    const raw = await readFile(filePath, "utf8");
    const parsed = JSON.parse(raw);
    if (!parsed || typeof parsed !== "object" || typeof parsed.entries !== "object") {
      return { version: 1, entries: {} };
    }
    return {
      version: 1,
      entries: Object.fromEntries(
        Object.entries(parsed.entries).filter(
          ([, value]) => value && typeof value === "object" && typeof value.disabled === "boolean" && typeof value.updatedAt === "string"
        )
      )
    };
  } catch {
    return { version: 1, entries: {} };
  }
}
function createFileToggleStore(filePath, options = {}) {
  let writeChain = Promise.resolve();
  async function queueWrite(operation) {
    const run = writeChain.catch(() => void 0).then(operation);
    writeChain = run.catch(() => void 0);
    await run;
  }
  async function writeToggleFile(next) {
    await mkdir(path.dirname(filePath), { recursive: true });
    await writeFile(filePath, JSON.stringify(next, null, 2), "utf8");
  }
  async function readPrimary() {
    return safeReadToggleFile(filePath);
  }
  async function readSecondary() {
    if (!options.secondaryReadOnlyPath) return { version: 1, entries: {} };
    return safeReadToggleFile(options.secondaryReadOnlyPath);
  }
  return {
    async isDisabled(sessionKey, agentId) {
      const resolved = await this.resolve(sessionKey, agentId);
      return resolved.disabled;
    },
    async resolve(sessionKey, agentId) {
      const key = encodeToggleKey(sessionKey, agentId);
      const primary = await readPrimary();
      if (primary.entries[key]) {
        return {
          disabled: primary.entries[key].disabled,
          source: "primary",
          updatedAt: primary.entries[key].updatedAt
        };
      }
      const secondary = await readSecondary();
      if (secondary.entries[key]) {
        return {
          disabled: secondary.entries[key].disabled,
          source: "secondary",
          updatedAt: secondary.entries[key].updatedAt
        };
      }
      return { disabled: false, source: "none" };
    },
    async setDisabled(sessionKey, agentId, disabled) {
      const key = encodeToggleKey(sessionKey, agentId);
      await queueWrite(async () => {
        const current = await readPrimary();
        current.entries[key] = {
          disabled,
          updatedAt: (/* @__PURE__ */ new Date()).toISOString()
        };
        await writeToggleFile(current);
      });
    },
    async clear(sessionKey, agentId) {
      const key = encodeToggleKey(sessionKey, agentId);
      await queueWrite(async () => {
        const current = await readPrimary();
        delete current.entries[key];
        await writeToggleFile(current);
      });
    },
    async list() {
      const current = await readPrimary();
      return Object.entries(current.entries).map(([key, value]) => {
        const decoded = decodeToggleKey(key);
        if (!decoded) return null;
        return {
          sessionKey: decoded.sessionKey,
          agentId: decoded.agentId,
          disabled: value.disabled,
          updatedAt: value.updatedAt
        };
      }).filter(
        (value) => value !== null
      );
    }
  };
}
export {
  createFileToggleStore
};
//# sourceMappingURL=session-toggles.js.map