"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSettings = exports.SettingsContext = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const appSettings_1 = require("../lib/appSettings");
const Context = (0, react_1.createContext)({
    // eslint-disable-next-line @typescript-eslint/no-unused-vars,arrow-body-style
    setOption: (name, value) => {
        console.log({
            name,
            value,
        });
        return;
    },
    settings: appSettings_1.DEFAULT_SETTINGS,
});
const SettingsContext = ({ children }) => {
    const [settings, setSettings] = (0, react_1.useState)(appSettings_1.DEFAULT_SETTINGS);
    const setOption = (0, react_1.useCallback)((setting, value) => {
        setSettings((options) => ({
            ...options,
            [setting]: value,
        }));
        if (appSettings_1.DEFAULT_SETTINGS[setting] === value) {
            setURLParam(setting, null);
        }
        else {
            setURLParam(setting, value);
        }
    }, []);
    const contextValue = (0, react_1.useMemo)(() => ({ setOption, settings }), [setOption, settings]);
    return React.createElement(Context.Provider, { value: contextValue }, children);
};
exports.SettingsContext = SettingsContext;
const useSettings = () => (0, react_1.useContext)(Context);
exports.useSettings = useSettings;
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