/*
 *  Copyright 2025 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.
 */

/**
 * This class provides a widget to select one or more contents depending on a catalog and a program.<br>
 * This embeds a drop down list with querying on title of contents and type-ahead support.<br> 
 * Advanced search through a dialog box could be enable. See #cfg-allowSearch.<br>
 * Allow content creation using #cfg-allowCreation.<br>
 * 
 * This widget allow to provide a catalog and a program, to limit the items to select to this given catalog and program
 * The catalog and program can be provided directly by their identifier, or by a relative path of form catalog or program field
 */
Ext.define('Ametys.odf.widget.SelectCatalogAndProgramAwareContent', {
    extend: 'Ametys.odf.widget.SelectCatalogAwareContent',
    
    /**
     * @cfg {String} [program] The identifier of the program to allow search on
     */
    /**
     * @property {String} _program The identifier of the program to allow search on. See #cfg-program
     * @private
     */
    
    /**
     * @cfg {String} [programField] The relative path of form program field. 
     */
    /**
     * @property {String} _programField The relative path of form program field. See #cfg-programField
     * @private
     */
    
    /**
     * @cfg {String} [targetProgramCriteria="reference-program-eq"] The name of the program criteria on targetted search model 
     */
    /**
     * @property {String} _targetProgramCriteria The name of the program criteria on targetted search model. See #cfg-targetProgramCriteria
     * @private
     */
    
    constructor: function(config) 
    {
        this.callParent(arguments);

        if (config.program)
        {
            this._program = config.program;
        }
        
        if (config.programField)
        {
            this._programField = config.programField;
            Ametys.form.Widget.onRelativeValueChange(this._programField, this, this._onProgramValueChange, this);
        }
        
        this._targetProgramCriteria = config.targetProgramCriteria || 'reference-program-eq';
    },
    
    _onProgramValueChange: function(relativePath, data, newValue, oldValue)
    {
        if (newValue != oldValue)
        {
            // Remove current value -> the selected content cannot be in the selected parent anymore since the parent changed
            this.reset();
            
            // Delete lastQuery to force load of store next time the combo is expanded
            delete this.combobox.lastQuery;
        }
    },

    _getAdditionalLoadValues: function()
    {
        var values = this.callParent(arguments);
        
        let programValue = this._getProgram();
        if (programValue)
        { 
            values[this._targetProgramCriteria] = programValue;
        }
        
        return values;
    },
    
    /**
     * Retrieves the program from the given identifier
     * @private
     */
    _getProgram: function ()
    {
        return  this._program || (this._programField ? Ametys.form.Widget.getRelativeValue(this._programField, this) : null);
    }
})