import '../../../babel/localeData.js';
import '../../../babel/babel-strings.js';
import '../../../chipper/js/browser/initialize-globals.js';
import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2022-2024, University of Colorado Boulder
/**
 * A universal locale Property that is accessible independently of the running Sim instance.
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import Property from '../../../axon/js/Property.js';
import StringUtils from '../../../phetcommon/js/util/StringUtils.js';
import { globalKeyStateTracker, KeyboardUtils } from '../../../scenery/js/imports.js';
import Tandem from '../../../tandem/js/Tandem.js';
import StringIO from '../../../tandem/js/types/StringIO.js';
import joist from '../joist.js';
import optionize from '../../../phet-core/js/optionize.js';
assert && assert(phet.chipper.locale, 'phet.chipper.locale global expected');
assert && assert(phet.chipper.localeData, 'phet.chipper.localeData global expected');
assert && assert(phet.chipper.strings, 'phet.chipper.strings global expected');
// Sort these properly by their localized name (without using _.sortBy, since string comparison does not provide
// a good sorting experience). See https://github.com/phetsims/joist/issues/965
const availableRuntimeLocales = Object.keys(phet.chipper.strings).sort((a, b) => {
    const lowerCaseA = StringUtils.localeToLocalizedName(a).toLowerCase();
    const lowerCaseB = StringUtils.localeToLocalizedName(b).toLowerCase();
    return lowerCaseA.localeCompare(lowerCaseB, 'en-US', { sensitivity: 'base' });
});
export class LocaleProperty extends Property {
    constructor(value, providedOptions) {
        const options = optionize()({
            valueType: 'string',
            phetioValueType: StringIO
        }, providedOptions);
        super(value, options);
        this.availableRuntimeLocales = availableRuntimeLocales;
    }
    // Override to provide grace and support for the full definition of allowed locales (aligned with the query parameter
    // schema). For example three letter values, and case insensitivity. See checkAndRemapLocale() for details. NOTE that
    // this will assert if the locale doesn't match the right format.
    unguardedSet(value) {
        // NOTE: updates phet.chipper.locale as a side-effect
        super.unguardedSet(phet.chipper.checkAndRemapLocale(value, true));
    }
    // This improves the PhET-iO Studio interface, by giving available values, without triggering validation if you want
    // to use the more general locale schema (three digit/case-insensitive/etc).
    toStateObject() {
        const parentObject = super.toStateObject();
        // Provide via validValues without forcing validation assertions if a different value is set.
        parentObject.validValues = [...this.availableRuntimeLocales].sort();
        return parentObject;
    }
    // Dynamic local switching is not supported if there is only one available runtime locale
    get supportsDynamicLocale() {
        return this.availableRuntimeLocales.length > 1;
    }
}
const localeProperty = new LocaleProperty(phet.chipper.locale, {
    tandem: Tandem.GENERAL_MODEL.createTandem('localeProperty'),
    phetioFeatured: true,
    phetioDocumentation: 'Specifies language currently displayed in the simulation'
});
if (window.phet?.chipper?.queryParameters?.keyboardLocaleSwitcher) {
    // DUPLICATION ALERT: don't change these without consulting PHET_IO_WRAPPERS/PhetioClient.initializeKeyboardLocaleSwitcher()
    const FORWARD_KEY = KeyboardUtils.KEY_I;
    const BACKWARD_KEY = KeyboardUtils.KEY_U;
    globalKeyStateTracker.keydownEmitter.addListener((event) => {
        const bump = (delta) => {
            // Ctrl + u in Chrome on Windows is "view source" in a new tab
            event.preventDefault();
            const index = availableRuntimeLocales.indexOf(localeProperty.value);
            const nextIndex = (index + delta + availableRuntimeLocales.length) % availableRuntimeLocales.length;
            localeProperty.value = availableRuntimeLocales[nextIndex];
            // Indicate the new locale on the console
            console.log(localeProperty.value);
        };
        if (event.ctrlKey && !event.shiftKey && !event.metaKey && !event.altKey) {
            if (KeyboardUtils.isKeyEvent(event, FORWARD_KEY)) {
                bump(+1);
            }
            else if (KeyboardUtils.isKeyEvent(event, BACKWARD_KEY)) {
                bump(-1);
            }
        }
    });
}
joist.register('localeProperty', localeProperty);
export default localeProperty;
//# sourceMappingURL=localeProperty.js.map