import '../../../assert/js/assert.js';
import '../../../globals.js';
import IOTypeCache from '../IOTypeCache.js';
import tandemNamespace from '../tandemNamespace.js';
import IOType from './IOType.js';
import StateSchema from './StateSchema.js';
// Cache each parameterized IOType so that it is only created once.
const cache = new IOTypeCache();
const joinKeys = (keys) => keys.join('|');
const EnumerationIO = (enumerationContainer) => {
    const enumeration = enumerationContainer.enumeration;
    // This caching implementation should be kept in sync with the other parametric IOType caching implementations.
    if (!cache.has(enumeration)) {
        // Enumeration supports additional documentation, so the values can be described.
        const additionalDocs = enumeration.phetioDocumentation ? ` ${enumeration.phetioDocumentation}` : '';
        const keys = enumeration.keys;
        const values = enumeration.values;
        const ioTypeName = `EnumerationIO(${joinKeys(keys)})`;
        assert && assert(!Array.from(cache.values()).find(ioType => ioType.typeName === ioTypeName), 'There was already another IOType with the same name: ' + ioTypeName);
        cache.set(enumeration, new IOType(ioTypeName, {
            validValues: values,
            documentation: `Possible values: ${keys.join(', ')}.${additionalDocs}`,
            toStateObject: (value) => enumeration.getKey(value),
            fromStateObject: (stateObject) => {
                assert && assert(typeof stateObject === 'string', 'unsupported EnumerationIO value type, expected string'); // eslint-disable-line phet/no-simple-type-checking-assertions
                assert && assert(keys.includes(stateObject), `Unrecognized value: ${stateObject}`);
                return enumeration.getValue(stateObject);
            },
            stateSchema: StateSchema.asValue(`${joinKeys(keys)}`, {
                isValidValue: (key) => keys.includes(key)
            })
        }));
    }
    return cache.get(enumeration);
};
tandemNamespace.register('EnumerationIO', EnumerationIO);
export default EnumerationIO;
//# sourceMappingURL=EnumerationIO.js.map