"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "OperatorsComponent", {
    enumerable: true,
    get: function() {
        return OperatorsComponent;
    }
});
const _element = require("../../../helpers/dom/element");
const _errors = require("../../../helpers/errors");
const _array = require("../../../helpers/array");
const _templateLiteralTag = require("../../../helpers/templateLiteralTag");
const _base = require("./_base");
const _logicalOperationRegisterer = require("../logicalOperationRegisterer");
const _conjunction = require("../logicalOperations/conjunction");
const _disjunction = require("../logicalOperations/disjunction");
const _disjunctionWithExtraCondition = require("../logicalOperations/disjunctionWithExtraCondition");
const _radioInput = require("../ui/radioInput");
function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) {
        throw new TypeError("Cannot initialize the same private elements twice on an object");
    }
}
function _class_private_method_get(receiver, privateSet, fn) {
    if (!privateSet.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return fn;
}
function _class_private_method_init(obj, privateSet) {
    _check_private_redeclaration(obj, privateSet);
    privateSet.add(obj);
}
const SELECTED_AT_START_ELEMENT_INDEX = 0;
var /**
   * OnChange listener.
   *
   * @param {Event} event The DOM event object.
   */ _onRadioInputChange = /*#__PURE__*/ new WeakSet();
class OperatorsComponent extends _base.BaseComponent {
    /**
   * Get menu object descriptor.
   *
   * @returns {object}
   */ getMenuItemDescriptor() {
        return {
            key: this.id,
            name: this.name,
            isCommand: false,
            disableSelection: true,
            hidden: ()=>this.isHidden(),
            renderer: (hot, wrapper)=>{
                (0, _element.addClass)(wrapper.parentNode, 'htFiltersMenuOperators');
                (0, _array.arrayEach)(this.elements, (ui)=>wrapper.appendChild(ui.element));
                return wrapper;
            }
        };
    }
    /**
   * Add RadioInputUI elements to component.
   *
   * @private
   */ buildOperatorsElement() {
        const operationKeys = [
            _conjunction.OPERATION_ID,
            _disjunction.OPERATION_ID
        ];
        (0, _array.arrayEach)(operationKeys, (operation)=>{
            const radioInput = new _radioInput.RadioInputUI(this.hot, {
                name: 'operator',
                label: {
                    htmlFor: operation,
                    textContent: (0, _logicalOperationRegisterer.getOperationName)(operation)
                },
                value: operation,
                checked: operation === operationKeys[SELECTED_AT_START_ELEMENT_INDEX],
                id: operation
            });
            radioInput.addLocalHook('change', (event)=>_class_private_method_get(this, _onRadioInputChange, onRadioInputChange).call(this, event));
            this.elements.push(radioInput);
        });
    }
    /**
   * Set state of operators component to check radio input at specific `index`.
   *
   * @param {number} searchedIndex Index of radio input to check.
   */ setChecked(searchedIndex) {
        if (this.elements.length < searchedIndex) {
            (0, _errors.throwWithCause)((0, _templateLiteralTag.toSingleLine)`Radio button with index ${searchedIndex} doesn't exist.`);
        }
        (0, _array.arrayEach)(this.elements, (element, index)=>{
            element.setChecked(index === searchedIndex);
        });
    }
    /**
   * Get `id` of active operator.
   *
   * @returns {string}
   */ getActiveOperationId() {
        const operationElement = this.elements.find((element)=>element instanceof _radioInput.RadioInputUI && element.isChecked());
        if (operationElement) {
            return operationElement.getValue();
        }
        return _conjunction.OPERATION_ID;
    }
    /**
   * Export state of the component (get selected operator).
   *
   * @returns {string} Returns `id` of selected operator.
   */ getState() {
        return this.getActiveOperationId();
    }
    /**
   * Set state of the component.
   *
   * @param {object} value State to restore.
   */ setState(value) {
        this.reset();
        if (value && this.getActiveOperationId() !== value) {
            (0, _array.arrayEach)(this.elements, (element)=>{
                element.setChecked(element.getValue() === value);
            });
        }
    }
    /**
   * Update state of component.
   *
   * @param {string} [operationId='conjunction'] Id of selected operation.
   * @param {number} column Physical column index.
   */ updateState(operationId = _conjunction.OPERATION_ID, column) {
        let selectedOperationId = operationId;
        if (selectedOperationId === _disjunctionWithExtraCondition.OPERATION_ID) {
            selectedOperationId = _disjunction.OPERATION_ID;
        }
        this.state.setValueAtIndex(column, selectedOperationId);
    }
    /**
   * Reset elements to their initial state.
   */ reset() {
        this.setChecked(SELECTED_AT_START_ELEMENT_INDEX);
    }
    constructor(hotInstance, options){
        super(hotInstance, {
            id: options.id,
            stateless: false
        }), _class_private_method_init(this, _onRadioInputChange), /**
   * The name of the component.
   *
   * @type {string}
   */ this.name = '';
        this.name = options.name;
        this.buildOperatorsElement();
    }
}
function onRadioInputChange(event) {
    this.setState(event.target.value);
}
