import * as React from 'react';
import { createContext, useCallback, useContext, useMemo, useState } from 'react';
import { DEFAULT_SETTINGS } from '../lib/appSettings';
const Context = createContext({
    // eslint-disable-next-line @typescript-eslint/no-unused-vars,arrow-body-style
    setOption: (name, value) => {
        console.log({
            name,
            value,
        });
        return;
    },
    settings: DEFAULT_SETTINGS,
});
export const SettingsContext = ({ children }) => {
    const [settings, setSettings] = useState(DEFAULT_SETTINGS);
    const setOption = useCallback((setting, value) => {
        setSettings((options) => ({
            ...options,
            [setting]: value,
        }));
        if (DEFAULT_SETTINGS[setting] === value) {
            setURLParam(setting, null);
        }
        else {
            setURLParam(setting, value);
        }
    }, []);
    const contextValue = useMemo(() => ({ setOption, settings }), [setOption, settings]);
    return React.createElement(Context.Provider, { value: contextValue }, children);
};
export const useSettings = () => useContext(Context);
function setURLParam(param, value) {
    const url = new URL(window.location.href);
    const params = new URLSearchParams(url.search);
    if (value !== null) {
        if (params.has(param)) {
            params.set(param, String(value));
        }
        else {
            params.append(param, String(value));
        }
    }
    else {
        if (params.has(param)) {
            params.delete(param);
        }
    }
    url.search = params.toString();
    window.history.pushState(null, '', url.toString());
}
//# sourceMappingURL=SettingsContext.js.map