import ngModule from '../../module';
import Component from '../../lib/component';
import './style.scss';

export default class Controller extends Component {
    constructor($element, $scope, $attrs) {
        super($element, $scope);
        this.hasInfo = Boolean($attrs.info);
        this.info = $attrs.info || null;
    }

    get model() {
        return this._model;
    }

    set model(value) {
        if (value === null) return;

        if (this.model === false && this.tripleState)
            value = null;

        this.emit('change', {value});

        this._model = value;
    }

    set field(value) {
        if (typeof value === 'number')
            value = Boolean(value);

        this._model = value;
    }

    get field() {
        return this._model;
    }

    get isIntermediate() {
        if (this.intermediate || (this.tripleState && (this.model === null || this.model === undefined)))
            return true;

        return false;
    }

    get isChecked() {
        return this.checked || this.model === true;
    }
}

Controller.$inject = ['$element', '$scope', '$attrs'];

ngModule.component('vnCheck', {
    template: require('./check.html'),
    controller: Controller,

    bindings: {
        field: '=?',
        label: '@?',
        disabled: '<?',
        checked: '<?',
        tripleState: '<?',
        intermediate: '<?'
    }
});