// Force strict mode and setup for ESM
"use strict";
import {
  readDaemonTrustPolicySnapshot
} from "./chunk-5SV5RW25.js";
import "./chunk-65735MAF.js";
import "./chunk-RKUWKYED.js";
import "./chunk-GOXKNSDZ.js";
import "./chunk-IDS7MSUP.js";
import "./chunk-RNNQYOBE.js";
import "./chunk-Y26TPMQ7.js";
import "./chunk-6PEXNQTX.js";
import {
  onTrustedFoldersChanged
} from "./chunk-4QPH7TD4.js";
import "./chunk-ALGGS7UH.js";
import "./chunk-MR3PXB6E.js";
import "./chunk-OWAK7LMW.js";
import {
  ideContextStore
} from "./chunk-HVFUW4JT.js";
import "./chunk-5M6IDOMF.js";
import "./chunk-TWPJO254.js";
import "./chunk-H6XPXXMH.js";
import "./chunk-AOHY3QJ3.js";
import "./chunk-5NNHICSA.js";
import "./chunk-SHN4KHDK.js";
import "./chunk-JXVC2PL3.js";
import "./chunk-KRXPRVGL.js";
import "./chunk-6PVPNMXU.js";
import "./chunk-OV2BALJJ.js";
import "./chunk-IRH27ZC2.js";
import "./chunk-QHWCP53L.js";
import "./chunk-TEGJSJDF.js";
import "./chunk-TIRRPCFS.js";
import "./chunk-O6GEWCJA.js";
import "./chunk-T26EAKDL.js";
import "./chunk-CPBF7KYF.js";
import "./chunk-4KP2F2TE.js";
import "./chunk-3APUUXRF.js";
import "./chunk-THVU3RNH.js";
import "./chunk-LHKCY7DY.js";
import "./chunk-6FMJLI5M.js";
import "./chunk-CBBP4BMC.js";
import "./chunk-S6LOFUVP.js";
import "./chunk-K623ENWT.js";
import "./chunk-AQ37AY7B.js";
import "./chunk-N4ACHWNH.js";
import "./chunk-ZPJWUGCS.js";
import "./chunk-D4HTUMGX.js";
import "./chunk-NAVJD2PQ.js";
import "./chunk-LOY7RWQG.js";
import "./chunk-E6CJ2SMM.js";
import "./chunk-DGEIYUC3.js";
import "./chunk-MPHPFVKK.js";
import "./chunk-GLNBDYUX.js";
import "./chunk-DJ2GSRLV.js";
import "./chunk-JHNMFZLP.js";
import "./chunk-Z3S5EMOW.js";
import "./chunk-2J3OJGTL.js";
import "./chunk-P3QQPMQA.js";
import "./chunk-XQK4XXTN.js";
import "./chunk-GLCZKT5V.js";
import "./chunk-QDK4F2N5.js";
import "./chunk-SCUZ3ZCL.js";
import "./chunk-VGC4I5JJ.js";
import "./chunk-3I6DUGQD.js";
import "./chunk-7ULQIS27.js";
import "./chunk-MM7TH7ZM.js";
import "./chunk-6WSXKTJR.js";
import "./chunk-46UV252V.js";
import "./chunk-ZBVS26BY.js";
import "./chunk-Q3T5KA2V.js";
import "./chunk-R2XWGR75.js";
import "./chunk-HC45LTEN.js";
import "./chunk-QARJNL2V.js";
import "./chunk-W4RQUHY3.js";
import "./chunk-G7XXKQYS.js";
import "./chunk-E2BHWIDT.js";
import "./chunk-VOQXFAY5.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
 */
