/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React from 'react';
import { settings } from './uiUtils';
const kDefaultTheme = 'system';
const kThemeSettingsKey = 'theme';
export const kThemeOptions = [
    { label: 'Dark mode', value: 'dark-mode' },
    { label: 'Light mode', value: 'light-mode' },
    { label: 'System', value: 'system' },
];
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
export function applyTheme() {
    if (document.playwrightThemeInitialized)
        return;
    document.playwrightThemeInitialized = true;
    document.defaultView.addEventListener('focus', (event) => {
        if (event.target.document.nodeType === Node.DOCUMENT_NODE)
            document.body.classList.remove('inactive');
    }, false);
    document.defaultView.addEventListener('blur', event => {
        document.body.classList.add('inactive');
    }, false);
    updateDocumentTheme(currentTheme());
    prefersDarkScheme.addEventListener('change', () => {
        updateDocumentTheme(currentTheme());
    });
}
const listeners = new Set();
function updateDocumentTheme(newTheme) {
    const oldDocumentTheme = currentDocumentTheme();
    const newDocumentTheme = newTheme === 'system'
        ? (prefersDarkScheme.matches ? 'dark-mode' : 'light-mode')
        : newTheme;
    if (oldDocumentTheme === newDocumentTheme)
        return;
    if (oldDocumentTheme)
        document.documentElement.classList.remove(oldDocumentTheme);
    document.documentElement.classList.add(newDocumentTheme);
    for (const listener of listeners)
        listener(newDocumentTheme);
}
export function addThemeListener(listener) {
    listeners.add(listener);
}
export function removeThemeListener(listener) {
    listeners.delete(listener);
}
function currentTheme() {
    return settings.getString(kThemeSettingsKey, kDefaultTheme);
}
export function currentDocumentTheme() {
    if (document.documentElement.classList.contains('dark-mode'))
        return 'dark-mode';
    if (document.documentElement.classList.contains('light-mode'))
        return 'light-mode';
    return null;
}
export function useThemeSetting() {
    const [theme, setTheme] = React.useState(currentTheme());
    React.useEffect(() => {
        settings.setString(kThemeSettingsKey, theme);
        updateDocumentTheme(theme);
    }, [theme]);
    return [theme, setTheme];
}
//# sourceMappingURL=theme.js.map