// Force strict mode and setup for ESM
"use strict";
import {
  readDaemonTrustPolicySnapshot
} from "./chunk-DJ7ATWB4.js";
import "./chunk-O3JAFRIR.js";
import "./chunk-RKUWKYED.js";
import "./chunk-GAPR7Q3F.js";
import "./chunk-4NSIRXOI.js";
import "./chunk-GOXKNSDZ.js";
import "./chunk-IDS7MSUP.js";
import "./chunk-6PEXNQTX.js";
import {
  onTrustedFoldersChanged
} from "./chunk-NHQGRBZG.js";
import "./chunk-ALGGS7UH.js";
import "./chunk-MR3PXB6E.js";
import "./chunk-OWAK7LMW.js";
import {
  ideContextStore
} from "./chunk-QQOPXQYM.js";
import "./chunk-TWPJO254.js";
import "./chunk-H6XPXXMH.js";
import "./chunk-5M6IDOMF.js";
import "./chunk-UPUG4GXM.js";
import "./chunk-64LQLPL7.js";
import "./chunk-2LQTH543.js";
import "./chunk-KRXPRVGL.js";
import "./chunk-GNCIZTNJ.js";
import "./chunk-CVHIL5IG.js";
import "./chunk-IRH27ZC2.js";
import "./chunk-QHWCP53L.js";
import "./chunk-PONHVBRT.js";
import "./chunk-T26EAKDL.js";
import "./chunk-6PVPNMXU.js";
import "./chunk-CPBF7KYF.js";
import "./chunk-6QIGFD5W.js";
import "./chunk-XWWKIXVD.js";
import "./chunk-75GFAX4R.js";
import "./chunk-OKF7JF3R.js";
import "./chunk-JSD33JG6.js";
import "./chunk-S6LOFUVP.js";
import "./chunk-JCW2R7WC.js";
import "./chunk-2DN4J57Y.js";
import "./chunk-ZPJWUGCS.js";
import "./chunk-2LKVPASC.js";
import "./chunk-NAVJD2PQ.js";
import "./chunk-LW3RFMG7.js";
import "./chunk-W4MIWP6M.js";
import "./chunk-DGEIYUC3.js";
import "./chunk-MPHPFVKK.js";
import "./chunk-GLNBDYUX.js";
import "./chunk-DJ2GSRLV.js";
import "./chunk-6D2RWQMA.js";
import "./chunk-75MIQ3LV.js";
import "./chunk-2J3OJGTL.js";
import "./chunk-P3QQPMQA.js";
import "./chunk-RPLGZJV2.js";
import "./chunk-GLCZKT5V.js";
import "./chunk-DZLYR4U3.js";
import "./chunk-NKHOVMWF.js";
import "./chunk-VGC4I5JJ.js";
import "./chunk-SGFT55XI.js";
import "./chunk-Y4IHBGBP.js";
import "./chunk-Y6AXW2OG.js";
import "./chunk-2M3XQKPR.js";
import "./chunk-46UV252V.js";
import "./chunk-ZBVS26BY.js";
import "./chunk-WZRWHXFE.js";
import "./chunk-A3NDJ5SB.js";
import "./chunk-7XATOOE7.js";
import "./chunk-BGTQCTQW.js";
import "./chunk-W4RQUHY3.js";
import "./chunk-S7W2GKIO.js";
import "./chunk-UHUSJ6X7.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
 */
