var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
/**
 * Custom createStrictContext to consolidate context-implementation across the system.
 * Unlike React's createContext, this throws an error by default when used outside a provider.
 *
 * Inspired by:
 * - https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
 * - https://github.com/chakra-ui/chakra-ui/blob/5ec0be610b5a69afba01a9c22365155c1b519136/packages/hooks/context/src/index.ts
 */
import React, { createContext as createReactContext, useContext as useReactContext, } from "react";
function getErrorMessage(name) {
    return `Aksel: use${name}Context returned \`undefined\`. Seems you forgot to wrap component within ${name}Provider`;
}
/* -----------------------------------------------------------------------------
 * Implementation
 * -------------------------------------------------------------------------- */
function createStrictContext(options) {
    const { name, defaultValue, errorMessage } = options;
    const hasDefault = "defaultValue" in options;
    const Context = createReactContext(defaultValue);
    Context.displayName = name;
    function Provider(_a) {
        var { children } = _a, context = __rest(_a, ["children"]);
        // biome-ignore lint/correctness/useExhaustiveDependencies: Object.values(context) includes all dependencies.
        const value = React.useMemo(() => context, Object.values(context)); // eslint-disable-line react-hooks/exhaustive-deps
        return React.createElement(Context.Provider, { value: value }, children);
    }
    Provider.displayName = `${name}Provider`;
    function useContext(strict = true, errorMessageOverride) {
        var _a, _b;
        const context = useReactContext(Context);
        if (!hasDefault && !context && strict) {
            const error = new Error((_a = errorMessageOverride !== null && errorMessageOverride !== void 0 ? errorMessageOverride : errorMessage) !== null && _a !== void 0 ? _a : getErrorMessage(name));
            error.name = "ContextError";
            (_b = Error.captureStackTrace) === null || _b === void 0 ? void 0 : _b.call(Error, error, useContext);
            throw error;
        }
        return context;
    }
    return { Provider, useContext };
}
export { createStrictContext };
//# sourceMappingURL=create-strict-context.js.map