/*
 *  Copyright 2015 Anyware Services
 *
 *  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.
 */

/**
 * Combobox showing all languages (to be used with a LanguageEnumerator).
 * The value can be set with the "CURRENT" string to use the current language.
 */
Ext.define('Ametys.cms.form.widget.SelectLanguage', {
    extend: 'Ametys.form.widget.ComboBox',
    
    setValue: function(value)
    {
        var val = value;
        
        if (value == 'CURRENT')
        {
            try
            {
                val = Ametys.cms.language.LanguageDAO.getCurrentLanguage();
            }
            catch (e)
            {
                // If there is a problem with LanguageDAO (like dependencies in some environments)
                val = null;
            }
        }
        
        this.callParent([val]);
    },
    
    getStoreCfg: function(config)
    {
        if (!config.enumeration)
        {
           try
            {
                config.enumeration = Ametys.cms.language.LanguageDAO.getStore().getData().items.map(l => [ l.getName(), l.getLabel() ]);
            }
            catch (e)
            {
                throw new Error("The language enumeration is not set and the LanguageDAO is not available.");
            }
        }
        return this.callParent(arguments);
    }
});