"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var runtime_store_exports = {};
__export(runtime_store_exports, {
  INVOCATION_UPDATE_EVENT: () => INVOCATION_UPDATE_EVENT,
  clearAssetWaitingState: () => clearAssetWaitingState,
  getAssetEditorRef: () => getAssetEditorRef,
  getAssetState: () => getAssetState,
  getInvocation: () => getInvocation,
  hasInvocationEffect: () => hasInvocationEffect,
  setAssetWaitingState: () => setAssetWaitingState,
  setAssetWidgetStatus: () => setAssetWidgetStatus,
  setInvocation: () => setInvocation,
  setInvocationEffect: () => setInvocationEffect,
  subscribeToInvocation: () => subscribeToInvocation
});
module.exports = __toCommonJS(runtime_store_exports);
const INVOCATION_UPDATE_EVENT = "ricos-assistant-invocation-update";
const getStores = () => {
  if (typeof window === "undefined") {
    return void 0;
  }
  window.RICOS ??= {};
  window.RICOS_INVOCATIONS ??= {};
  return {
    assets: window.RICOS,
    invocations: window.RICOS_INVOCATIONS
  };
};
const getAssetState = (assetId) => {
  return getStores()?.assets[assetId];
};
const getAssetEditorRef = (assetId) => getAssetState(assetId)?.editorRef;
const setAssetWaitingState = (assetId) => {
  const stores = getStores();
  if (!stores) {
    return;
  }
  const asset = stores.assets[assetId] ?? (stores.assets[assetId] = {});
  asset.isLoadingFromWidget = true;
  asset.setIsWaitingForStream?.(true);
};
const clearAssetWaitingState = (assetId) => {
  const asset = getAssetState(assetId);
  if (asset) {
    asset.setIsWaitingForStream?.(false);
    asset.isLoadingFromWidget = false;
  }
};
const setAssetWidgetStatus = (assetId, status) => getAssetState(assetId)?.updateAssistantWidgetStatus?.(status);
const getInvocation = (invocationId) => {
  return getStores()?.invocations[invocationId];
};
const setInvocation = (invocationId, state) => {
  const stores = getStores();
  if (!stores) {
    return state;
  }
  stores.invocations[invocationId] = state;
  window.dispatchEvent(
    new CustomEvent(INVOCATION_UPDATE_EVENT, {
      detail: { invocationId, state }
    })
  );
  return state;
};
const updateStoredInvocation = (invocationId, updater) => {
  const current = getInvocation(invocationId);
  if (!current) {
    return void 0;
  }
  const next = updater(current);
  if (next === current) {
    return current;
  }
  return setInvocation(invocationId, next);
};
const hasInvocationEffect = (invocationId, effect) => Boolean(getInvocation(invocationId)?.effects?.[effect]);
const setInvocationEffect = (invocationId, effect) => updateStoredInvocation(invocationId, (current) => {
  if (current.effects?.[effect]) {
    return current;
  }
  return {
    ...current,
    effects: {
      ...current.effects,
      [effect]: true
    }
  };
});
const subscribeToInvocation = (invocationId, listener) => {
  if (typeof window === "undefined") {
    return () => {
    };
  }
  const handleUpdate = (event) => {
    const detail = event.detail;
    if (detail?.invocationId === invocationId) {
      listener(detail.state);
    }
  };
  window.addEventListener(INVOCATION_UPDATE_EVENT, handleUpdate);
  return () => {
    window.removeEventListener(INVOCATION_UPDATE_EVENT, handleUpdate);
  };
};
//# sourceMappingURL=runtime-store.js.map
