import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useMemo } from "react";
import { ok } from "../../../is.js";
import { TimezoneProvider } from "../../client/components/happening/index.js";
const TRUSTED_ROLE = ["moderator", "admin", "developer"];
export const DataContext = createContext(undefined);
export function DataProvider({ children, ...props }) {
    return (_jsx(DataContext.Provider, { ...props, children: _jsx(TimezoneProvider, { value: props.value.timezone, children: children }) }));
}
export function useData() {
    const context = useContext(DataContext);
    ok(context, "Expected DataProvider to be used");
    return context;
}
export function useMaybeUser() {
    const { user } = useData();
    return user;
}
export function useUser() {
    const user = useMaybeUser();
    ok(user, "Expected user");
    return user;
}
export function useMaybeAuthenticationState() {
    const { authenticationState } = useData();
    return authenticationState;
}
export function useAuthenticationState() {
    const authenticationState = useMaybeAuthenticationState();
    ok(authenticationState, "Expected authenticationState");
    return authenticationState;
}
export function useMaybeBody() {
    const { body } = useData();
    if (!body)
        return undefined;
    ok(body);
    return body;
}
export function useBody() {
    const body = useMaybeBody();
    ok(body, "Expected body");
    return body;
}
export function useQuery() {
    const { query } = useData();
    const queryValue = query || {};
    ok(queryValue, "Expected query");
    return queryValue;
}
export function useQuerySearch() {
    const query = useQuery();
    return query.search || query.productName || "";
}
export function useMaybeResult() {
    const { result } = useData();
    if (!result)
        return undefined;
    ok(result);
    return result;
}
export function useResult() {
    const result = useMaybeResult();
    ok(result, "Expected result");
    return result;
}
export function useMaybeInput() {
    const { input } = useData();
    if (!input)
        return undefined;
    ok(input);
    return input;
}
export function useInput() {
    const input = useMaybeInput();
    ok(input, "Expected input");
    return input;
}
export function useError() {
    const { error } = useData();
    return error;
}
export function useSubmitted() {
    const { submitted } = useData();
    return !!submitted;
}
export function usePartners() {
    const { partners } = useData();
    return useMemo(() => partners || [], [partners]);
}
export function usePartner(partnerId) {
    const partners = usePartners();
    return useMemo(() => partners.find((partner) => partner.partnerId === partnerId), [partners]);
}
export function useOrganisations() {
    const { organisations } = useData();
    return organisations;
}
export function useRoles() {
    const { roles } = useData();
    return useMemo(() => roles ?? [], [roles]);
}
export function useIsRole(role) {
    const roles = useRoles();
    return useMemo(() => isRole(roles, role), [roles, role]);
}
function isRole(roles, role) {
    if (!roles)
        return false;
    return roles.includes(role);
}
export function useIsTrusted() {
    const roles = useRoles();
    return useMemo(() => {
        return !!TRUSTED_ROLE.find((role) => isRole(roles, role));
    }, [roles]);
}
export function useIsModerator() {
    return useIsRole("moderator");
}
export function useIsAdmin() {
    return useIsRole("admin");
}
export function useTimezone() {
    const { timezone } = useData();
    return timezone;
}
export function useConfig() {
    const { config } = useData();
    return config;
}
//# sourceMappingURL=provider.js.map