'use client';


import { localeInStorage, setLocaleInStorage } from "./useLocaleStorage.mjs";
import { localeResolver } from "../@intlayer/core/dist/esm/localization/localeResolver.mjs";
import { MessageKey } from "../@intlayer/editor-react/dist/esm/editor/dist/esm/messagesKeys.mjs";
import { useCrossFrameState } from "../@intlayer/editor-react/dist/esm/useCrossFrameState.mjs";
import { built_default } from "../@intlayer/config/dist/esm/built.mjs";
import { IntlayerEditorProvider } from "../editor/IntlayerEditorProvider.mjs";
import { createContext, useContext } from "react";
import { jsx } from "react/jsx-runtime";

//#region ../react-intlayer/dist/esm/client/IntlayerProvider.mjs
/**
* Context that store the current locale on the client side
*/
const IntlayerClientContext = createContext({
	locale: localeInStorage ?? built_default?.internationalization?.defaultLocale,
	setLocale: () => null,
	isCookieEnabled: true,
	disableEditor: false
});
/**
* Hook that provides the current locale
*/
const useIntlayerContext = () => 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 } = built_default ?? {};
	const { defaultLocale: defaultLocaleConfig, locales: availableLocales } = internationalization ?? {};
	const defaultLocale = localeProp ?? localeInStorage ?? defaultLocaleProp ?? defaultLocaleConfig;
	const [currentLocale, setCurrentLocale] = useCrossFrameState(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);
		setLocaleInStorage(newLocale, isCookieEnabled);
	};
	const setLocale = setLocaleProp ?? setLocaleBase;
	const resolvedLocale = localeResolver(localeProp ?? currentLocale);
	return /* @__PURE__ */ jsx(IntlayerClientContext.Provider, {
		value: {
			locale: resolvedLocale,
			setLocale,
			disableEditor
		},
		children
	});
};
const IntlayerProvider = (props) => /* @__PURE__ */ jsx(IntlayerEditorProvider, { children: /* @__PURE__ */ jsx(IntlayerProviderContent, { ...props }) });

//#endregion
export { IntlayerClientContext, IntlayerProvider, useIntlayerContext };
//# sourceMappingURL=IntlayerProvider.mjs.map