(function () {
  'use strict';

  // Generated by @auroraview/sdk

  // src/inject/state_bridge.ts
  (function() {
    const _stateData = {};
    const _changeHandlers = [];
    function notifyHandlers(key, value, source) {
      _changeHandlers.forEach((handler) => {
        try {
          handler(key, value, source);
        } catch (e) {
          console.error("[AuroraView State] Handler error:", e);
        }
      });
    }
    function sendToPython(key, value) {
      if (window.auroraview && window.auroraview.send_event) {
        window.auroraview.send_event("__state_update__", { key, value });
      }
    }
    function createStateProxy() {
      return new Proxy(_stateData, {
        get: function(target, prop) {
          if (prop === "onChange") {
            return function(handler) {
              _changeHandlers.push(handler);
              return function() {
                const idx = _changeHandlers.indexOf(handler);
                if (idx > -1) _changeHandlers.splice(idx, 1);
              };
            };
          }
          if (prop === "offChange") {
            return function(handler) {
              const idx = _changeHandlers.indexOf(handler);
              if (idx > -1) _changeHandlers.splice(idx, 1);
            };
          }
          if (prop === "toJSON") {
            return function() {
              return Object.assign({}, target);
            };
          }
          if (prop === "keys") {
            return function() {
              return Object.keys(target);
            };
          }
          return target[prop];
        },
        set: function(target, prop, value) {
          const oldValue = target[prop];
          target[prop] = value;
          if (oldValue !== value) {
            sendToPython(prop, value);
            notifyHandlers(prop, value, "javascript");
          }
          return true;
        },
        deleteProperty: function(target, prop) {
          if (prop in target) {
            delete target[prop];
            sendToPython(prop, void 0);
            notifyHandlers(prop, void 0, "javascript");
          }
          return true;
        }
      });
    }
    const stateProxy = createStateProxy();
    function handleStateSync(data) {
      if (!data || typeof data !== "object") return;
      switch (data.type) {
        case "set":
          if (data.key) {
            _stateData[data.key] = data.value;
            notifyHandlers(data.key, data.value, "python");
          }
          break;
        case "delete":
          if (data.key) {
            delete _stateData[data.key];
            notifyHandlers(data.key, void 0, "python");
          }
          break;
        case "batch":
          if (data.data && typeof data.data === "object") {
            Object.entries(data.data).forEach(([key, value]) => {
              _stateData[key] = value;
              notifyHandlers(key, value, "python");
            });
          }
          break;
        case "full":
          Object.keys(_stateData).forEach((key) => delete _stateData[key]);
          if (data.data && typeof data.data === "object") {
            Object.assign(_stateData, data.data);
            Object.entries(data.data).forEach(([key, value]) => {
              notifyHandlers(key, value, "python");
            });
          }
          break;
        case "clear":
          const keys = Object.keys(_stateData);
          keys.forEach((key) => {
            delete _stateData[key];
            notifyHandlers(key, void 0, "python");
          });
          break;
      }
    }
    if (window.auroraview) {
      window.auroraview.state = stateProxy;
      window.auroraview.on("__state_sync__", handleStateSync);
    } else {
      Object.defineProperty(window, "auroraview", {
        configurable: true,
        set: function(val) {
          delete window.auroraview;
          window.auroraview = val;
          window.auroraview.state = stateProxy;
          window.auroraview.on("__state_sync__", handleStateSync);
        }
      });
    }
    console.log("[AuroraView] State bridge initialized");
  })();

})();
