require("../../_virtual/_rolldown/runtime.js");
const require_defineProperty = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_useSync = require("./hooks/useSync.js");
let react = require("react");
//#region src/components/ticketBooking/miniRedux.tsx
var Store = class {
	constructor(initialState) {
		require_defineProperty._defineProperty(this, "state", void 0);
		require_defineProperty._defineProperty(this, "listeners", void 0);
		this.state = initialState;
		this.listeners = /* @__PURE__ */ new Set();
	}
	getState() {
		return this.state;
	}
	setState(partialState, callback, key) {
		console.log("partialState", partialState);
		if (typeof partialState === "function") Object.assign(this.state, partialState(this.state));
		else Object.assign(this.state, partialState);
		this.notify(key);
		if (callback) callback(this.state);
	}
	subscribe(listener) {
		this.listeners.add(listener);
		return () => {
			this.listeners.delete(listener);
		};
	}
	notify(key) {
		for (const listener of this.listeners) listener(key);
	}
	getLatestState() {
		return this.state;
	}
};
const StoreContext = (0, react.createContext)(null);
function Provider({ store, children }) {
	return /* @__PURE__ */ React.createElement(StoreContext.Provider, { value: store }, children);
}
function createStore(initialState) {
	return new Store(initialState);
}
function useStore() {
	var _context$appHelper, _context$appHelper$ge, _app$getPlugin, _app$getPlugin$useRun;
	const context = require_useEngineContext.default();
	const app = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 ? void 0 : _context$appHelper$ge.call(_context$appHelper);
	const { runtime } = (app === null || app === void 0 || (_app$getPlugin = app.getPlugin) === null || _app$getPlugin === void 0 || (_app$getPlugin = _app$getPlugin.call(app, "unifiedCommunication")) === null || _app$getPlugin === void 0 || (_app$getPlugin$useRun = _app$getPlugin.useRuntime) === null || _app$getPlugin$useRun === void 0 ? void 0 : _app$getPlugin$useRun.call(_app$getPlugin)) || {};
	const store = (0, react.useContext)(StoreContext);
	if (!store) throw new Error("useStore must be used within a Provider");
	const stateRef = (0, react.useRef)(store.getState());
	const [, forceUpdate] = (0, react.useState)({});
	(0, react.useEffect)(() => {
		return store.subscribe(() => {
			stateRef.current = store.getState();
			forceUpdate({});
		});
	}, [store, runtime]);
	require_useSync.useSync(store);
	(0, react.useEffect)(() => {
		window._bookingStore = store;
	}, []);
	return [stateRef.current, store.setState.bind(store)];
}
//#endregion
exports.Provider = Provider;
exports.createStore = createStore;
exports.useStore = useStore;
