'use client';


const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_useLocaleStorage = require('./useLocaleStorage.cjs');
const require_localeResolver = require('../@intlayer/core/dist/esm/localization/localeResolver.cjs');
const require_messagesKeys = require('../@intlayer/editor-react/dist/esm/editor/dist/esm/messagesKeys.cjs');
const require_useCrossFrameState = require('../@intlayer/editor-react/dist/esm/useCrossFrameState.cjs');
const require_built = require('../@intlayer/config/dist/esm/built.cjs');
const require_IntlayerEditorProvider = require('../editor/IntlayerEditorProvider.cjs');
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");

//#region ../react-intlayer/dist/esm/client/IntlayerProvider.mjs
/**
* Context that store the current locale on the client side
*/
const IntlayerClientContext = (0, react.createContext)({
	locale: require_useLocaleStorage.localeInStorage ?? require_built.built_default?.internationalization?.defaultLocale,
	setLocale: () => null,
	isCookieEnabled: true,
	disableEditor: false
});
/**
* Hook that provides the current locale
*/
const useIntlayerContext = () => (0, react.useContext)(IntlayerClientContext);
/**
* Provider that store the current locale on the client side
*/
const IntlayerProviderContent = ({ locale: localeProp, defaultLocale: defaultLocaleProp, children, setLocale: setLocaleProp, disableEditor, isCookieEnabled }) => {
	const { internationalization } = require_built.built_default ?? {};
	const { defaultLocale: defaultLocaleConfig, locales: availableLocales } = internationalization ?? {};
	const defaultLocale = localeProp ?? require_useLocaleStorage.localeInStorage ?? defaultLocaleProp ?? defaultLocaleConfig;
	const [currentLocale, setCurrentLocale] = require_useCrossFrameState.useCrossFrameState(require_messagesKeys.MessageKey.INTLAYER_CURRENT_LOCALE, defaultLocale);
	const setLocaleBase = (newLocale) => {
		if (currentLocale.toString() === newLocale.toString()) return;
		if (!availableLocales?.map(String).includes(newLocale)) {
			console.error(`Locale ${newLocale} is not available`);
			return;
		}
		setCurrentLocale(newLocale);
		require_useLocaleStorage.setLocaleInStorage(newLocale, isCookieEnabled);
	};
	const setLocale = setLocaleProp ?? setLocaleBase;
	const resolvedLocale = require_localeResolver.localeResolver(localeProp ?? currentLocale);
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IntlayerClientContext.Provider, {
		value: {
			locale: resolvedLocale,
			setLocale,
			disableEditor
		},
		children
	});
};
const IntlayerProvider = (props) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_IntlayerEditorProvider.IntlayerEditorProvider, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IntlayerProviderContent, { ...props }) });

//#endregion
exports.IntlayerClientContext = IntlayerClientContext;
exports.IntlayerProvider = IntlayerProvider;
exports.useIntlayerContext = useIntlayerContext;
//# sourceMappingURL=IntlayerProvider.cjs.map