/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.0.5
 * @link 
 * @license 
 */
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var events_1 = require("../events");
var dyAbstractInputField_1 = require("./dyAbstractInputField");
var dom_1 = require("../utils/dom");
var DyCheckbox = /** @class */ (function (_super) {
    __extends(DyCheckbox, _super);
    function DyCheckbox(config, className, inputType) {
        if (className === void 0) { className = 'dy-checkbox'; }
        if (inputType === void 0) { inputType = 'checkbox'; }
        var _this = _super.call(this, config, className, inputType) || this;
        _this.labelAlignment = 'right';
        _this.selected = false;
        _this.readOnly = false;
        _this.passive = false;
        return _this;
    }
    DyCheckbox.prototype.addInputListeners = function () {
        this.addManagedListener(this.eInput, 'click', this.onCheckboxClick.bind(this));
        this.addManagedListener(this.eLabel, 'click', this.toggle.bind(this));
    };
    DyCheckbox.prototype.getNextValue = function () {
        return this.selected === undefined ? true : !this.selected;
    };
    DyCheckbox.prototype.setPassive = function (passive) {
        this.passive = passive;
    };
    DyCheckbox.prototype.isReadOnly = function () {
        return this.readOnly;
    };
    DyCheckbox.prototype.setReadOnly = function (readOnly) {
        dom_1.addOrRemoveCssClass(this.eWrapper, 'dy-disabled', readOnly);
        this.eInput.disabled = readOnly;
        this.readOnly = readOnly;
    };
    DyCheckbox.prototype.setDisabled = function (disabled) {
        dom_1.addOrRemoveCssClass(this.eWrapper, 'dy-disabled', disabled);
        return _super.prototype.setDisabled.call(this, disabled);
    };
    DyCheckbox.prototype.toggle = function () {
        var previousValue = this.isSelected();
        var nextValue = this.getNextValue();
        if (this.passive) {
            this.dispatchChange(nextValue, previousValue);
        }
        else {
            this.setValue(nextValue);
        }
    };
    DyCheckbox.prototype.getValue = function () {
        return this.isSelected();
    };
    DyCheckbox.prototype.setValue = function (value, silent) {
        this.refreshSelectedClass(value);
        this.setSelected(value, silent);
        return this;
    };
    DyCheckbox.prototype.setName = function (name) {
        var input = this.getInputElement();
        input.name = name;
        return this;
    };
    DyCheckbox.prototype.isSelected = function () {
        return this.selected;
    };
    DyCheckbox.prototype.setSelected = function (selected, silent) {
        if (this.isSelected() === selected) {
            return;
        }
        this.previousValue = this.isSelected();
        selected = this.selected = typeof selected === 'boolean' ? selected : undefined;
        this.eInput.checked = selected;
        this.eInput.indeterminate = selected === undefined;
        if (!silent) {
            this.dispatchChange(this.selected, this.previousValue);
        }
    };
    DyCheckbox.prototype.dispatchChange = function (selected, previousValue, event) {
        this.dispatchEvent({ type: DyCheckbox.EVENT_CHANGED, selected: selected, previousValue: previousValue, event: event });
        var input = this.getInputElement();
        var checkboxChangedEvent = {
            type: events_1.Events.EVENT_CHECKBOX_CHANGED,
            id: input.id,
            name: input.name,
            selected: selected,
            previousValue: previousValue
        };
        this.eventService.dispatchEvent(checkboxChangedEvent);
    };
    DyCheckbox.prototype.onCheckboxClick = function (e) {
        if (this.passive) {
            return;
        }
        var previousValue = this.isSelected();
        var selected = this.selected = e.target.checked;
        this.refreshSelectedClass(selected);
        this.dispatchChange(selected, previousValue, e);
    };
    DyCheckbox.prototype.refreshSelectedClass = function (value) {
        dom_1.addOrRemoveCssClass(this.eWrapper, 'dy-checked', value === true);
        dom_1.addOrRemoveCssClass(this.eWrapper, 'dy-indeterminate', value == null);
    };
    return DyCheckbox;
}(dyAbstractInputField_1.DyAbstractInputField));
exports.DyCheckbox = DyCheckbox;

//# sourceMappingURL=dyCheckbox.js.map
