// Force strict mode and setup for ESM
"use strict";
import {
  readDaemonTrustPolicySnapshot
} from "./chunk-UVJHXHRX.js";
import "./chunk-P7RKN76O.js";
import "./chunk-RKUWKYED.js";
import "./chunk-GOXKNSDZ.js";
import "./chunk-IDS7MSUP.js";
import "./chunk-FAH5P34O.js";
import "./chunk-XY77A4MO.js";
import "./chunk-6PEXNQTX.js";
import {
  onTrustedFoldersChanged
} from "./chunk-OAW4O3MF.js";
import "./chunk-ALGGS7UH.js";
import "./chunk-MR3PXB6E.js";
import {
  ideContextStore
} from "./chunk-VDNAD6JC.js";
import "./chunk-GOFAQQZA.js";
import "./chunk-5M6IDOMF.js";
import "./chunk-TWPJO254.js";
import "./chunk-H6XPXXMH.js";
import "./chunk-VA5NLVPT.js";
import "./chunk-I7JDGNG6.js";
import "./chunk-A6SX5TAH.js";
import "./chunk-33FNCQSY.js";
import "./chunk-6PVPNMXU.js";
import "./chunk-QP2X3YUW.js";
import "./chunk-IRH27ZC2.js";
import "./chunk-QHWCP53L.js";
import "./chunk-MOHAWIAW.js";
import "./chunk-X6QNZOTC.js";
import "./chunk-O6GEWCJA.js";
import "./chunk-T26EAKDL.js";
import "./chunk-ZPJWUGCS.js";
import "./chunk-CPBF7KYF.js";
import "./chunk-MISUEWB4.js";
import "./chunk-KRXPRVGL.js";
import "./chunk-A5YZ3FA2.js";
import "./chunk-YAVY33G4.js";
import "./chunk-2UISUFHQ.js";
import "./chunk-HFVW54NL.js";
import "./chunk-TZTBZ25J.js";
import "./chunk-S6LOFUVP.js";
import "./chunk-SRY26X4H.js";
import "./chunk-K623ENWT.js";
import "./chunk-AQ37AY7B.js";
import "./chunk-432XCUHF.js";
import "./chunk-XC656O42.js";
import "./chunk-NAVJD2PQ.js";
import "./chunk-QYPT3QUD.js";
import "./chunk-QMBEW7EW.js";
import "./chunk-DGEIYUC3.js";
import "./chunk-MPHPFVKK.js";
import "./chunk-GLNBDYUX.js";
import "./chunk-DJ2GSRLV.js";
import "./chunk-5CPNLKAW.js";
import "./chunk-RPYYTL7E.js";
import "./chunk-2J3OJGTL.js";
import "./chunk-P3QQPMQA.js";
import "./chunk-6O5DVH27.js";
import "./chunk-GLCZKT5V.js";
import "./chunk-2DNKEBXY.js";
import "./chunk-N7ZZBTIU.js";
import "./chunk-VGC4I5JJ.js";
import "./chunk-E4WHKETF.js";
import "./chunk-UEJESCS7.js";
import "./chunk-CFDR3GNL.js";
import "./chunk-O4Z6JR7D.js";
import "./chunk-46UV252V.js";
import "./chunk-ZBVS26BY.js";
import "./chunk-BFHXDAR7.js";
import "./chunk-TYERNYH4.js";
import "./chunk-UH7Z7Y45.js";
import "./chunk-O2ZYHG33.js";
import "./chunk-W4RQUHY3.js";
import "./chunk-G7XXKQYS.js";
import "./chunk-3RXUYQJI.js";
import "./chunk-VOQXFAY5.js";
import "./chunk-OWAK7LMW.js";
import "./chunk-75DOP5OR.js";
import "./chunk-DMTGGOSA.js";
import "./chunk-YQ3U5MUC.js";
import "./chunk-AMDSOFFV.js";
import "./chunk-QUGAUQGJ.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/config/daemon-trust-policy-monitor.ts
init_esbuild_shims();
function createDaemonTrustPolicyMonitor(options) {
  const readSnapshot = options.readSnapshot ?? readDaemonTrustPolicySnapshot;
  const pollIntervalMs = options.pollIntervalMs ?? 1e3;
  const pendingReasons = /* @__PURE__ */ new Set();
  let started = false;
  let stopped = false;
  let lastRevision;
  let timer;
  let unsubscribeIde;
  let unsubscribeTrustedFolders;
  let running;
  const drain = /* @__PURE__ */ __name(async () => {
    while (!stopped && pendingReasons.size > 0) {
      const reasons = new Set(pendingReasons);
      pendingReasons.clear();
      try {
        const snapshot = await readSnapshot();
        if (stopped) return;
        if (snapshot.revision !== lastRevision || reasons.has("manual")) {
          await options.onSnapshot(snapshot, reasons);
          lastRevision = snapshot.revision;
        }
      } catch (error) {
        options.onError?.(error);
      }
    }
  }, "drain");
  const startDrain = /* @__PURE__ */ __name(() => {
    running = drain().finally(() => {
      running = void 0;
      if (!stopped && pendingReasons.size > 0) {
        return startDrain();
      }
      return void 0;
    });
    return running;
  }, "startDrain");
  const requestReconcile = /* @__PURE__ */ __name((reason = "manual") => {
    if (stopped) return Promise.resolve();
    pendingReasons.add(reason);
    return running ?? startDrain();
  }, "requestReconcile");
  return {
    async start() {
      if (started) {
        await running;
        return;
      }
      started = true;
      unsubscribeIde = ideContextStore.subscribe(() => {
        void requestReconcile("ide");
      });
      unsubscribeTrustedFolders = onTrustedFoldersChanged(() => {
        void requestReconcile("trusted_folders");
      });
      timer = setInterval(() => {
        void requestReconcile("poll");
      }, pollIntervalMs);
      timer.unref?.();
      await requestReconcile("initial");
    },
    requestReconcile,
    stop() {
      if (stopped) return;
      stopped = true;
      pendingReasons.clear();
      if (timer) clearInterval(timer);
      unsubscribeIde?.();
      unsubscribeTrustedFolders?.();
    }
  };
}
__name(createDaemonTrustPolicyMonitor, "createDaemonTrustPolicyMonitor");
export {
  createDaemonTrustPolicyMonitor
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
