import { ChangeDetectorRef, Component, ElementRef, EventEmitter, forwardRef, HostListener, Input, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Config } from '../../config/config';
import { Form } from '../../util/form';
import { Ion } from '../ion';
import { isTrueProperty } from '../../util/util';
import { Item } from '../item/item';
export const /** @type {?} */ CHECKBOX_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => Checkbox),
    multi: true
};
/**
 * \@name Checkbox
 * \@module ionic
 *
 * \@description
 * The Checkbox is a simple component styled based on the mode. It can be
 * placed in an `ion-item` or used as a stand-alone checkbox.
 *
 * See the [Angular 2 Docs](https://angular.io/docs/ts/latest/guide/forms.html)
 * for more info on forms and inputs.
 *
 *
 * \@usage
 * ```html
 *
 *  <ion-list>
 *
 *    <ion-item>
 *      <ion-label>Pepperoni</ion-label>
 *      <ion-checkbox [(ngModel)]="pepperoni"></ion-checkbox>
 *    </ion-item>
 *
 *    <ion-item>
 *      <ion-label>Sausage</ion-label>
 *      <ion-checkbox [(ngModel)]="sausage" disabled="true"></ion-checkbox>
 *    </ion-item>
 *
 *    <ion-item>
 *      <ion-label>Mushrooms</ion-label>
 *      <ion-checkbox [(ngModel)]="mushrooms"></ion-checkbox>
 *    </ion-item>
 *
 *  </ion-list>
 * ```
 *
 * \@demo /docs/v2/demos/src/checkbox/
 * @see {\@link /docs/v2/components#checkbox Checkbox Component Docs}
 */
export class Checkbox extends Ion {
    /**
     * @param {?} config
     * @param {?} _form
     * @param {?} _item
     * @param {?} elementRef
     * @param {?} renderer
     * @param {?} _cd
     */
    constructor(config, _form, _item, elementRef, renderer, _cd) {
        super(config, elementRef, renderer, 'checkbox');
        this._form = _form;
        this._item = _item;
        this._cd = _cd;
        /**
         * @hidden
         */
        this._checked = false;
        /**
         * @hidden
         */
        this._disabled = false;
        /**
         * \@output {Checkbox} Emitted when the checkbox value changes.
         */
        this.ionChange = new EventEmitter();
        _form.register(this);
        if (_item) {
            this.id = 'chk-' + _item.registerInput('checkbox');
            this._labelId = 'lbl-' + _item.id;
            this._item.setElementClass('item-checkbox', true);
        }
    }
    /**
     * @hidden
     * @param {?} ev
     * @return {?}
     */
    _click(ev) {
        (void 0) /* console.debug */;
        ev.preventDefault();
        ev.stopPropagation();
        this.onChange(!this._checked);
    }
    /**
     * \@input {boolean} If true, the element is selected.
     * @return {?}
     */
    get checked() {
        return this._checked;
    }
    /**
     * @param {?} val
     * @return {?}
     */
    set checked(val) {
        this._setChecked(isTrueProperty(val));
        this.onChange(this._checked);
    }
    /**
     * @hidden
     * @param {?} isChecked
     * @return {?}
     */
    _setChecked(isChecked) {
        if (isChecked !== this._checked) {
            this._checked = isChecked;
            if (this._init) {
                this.ionChange.emit(this);
            }
            this._item && this._item.setElementClass('item-checkbox-checked', isChecked);
        }
    }
    /**
     * @hidden
     * @param {?} val
     * @return {?}
     */
    writeValue(val) {
        this._setChecked(isTrueProperty(val));
    }
    /**
     * @hidden
     * @param {?} fn
     * @return {?}
     */
    registerOnChange(fn) {
        this._fn = fn;
        this.onChange = (isChecked) => {
            (void 0) /* console.debug */;
            fn(isChecked);
            this._setChecked(isChecked);
            this.onTouched();
            this._cd.detectChanges();
        };
    }
    /**
     * @hidden
     * @param {?} fn
     * @return {?}
     */
    registerOnTouched(fn) { this.onTouched = fn; }
    /**
     * \@input {boolean} If true, the user cannot interact with this element.
     * @return {?}
     */
    get disabled() {
        return this._disabled;
    }
    /**
     * @param {?} val
     * @return {?}
     */
    set disabled(val) {
        this._disabled = isTrueProperty(val);
        this._item && this._item.setElementClass('item-checkbox-disabled', this._disabled);
    }
    /**
     * @hidden
     * @param {?} isChecked
     * @return {?}
     */
    onChange(isChecked) {
        // used when this input does not have an ngModel or formControlName
        (void 0) /* console.debug */;
        this._setChecked(isChecked);
        this.onTouched();
        this._cd.detectChanges();
    }
    /**
     * @hidden
     * @return {?}
     */
    initFocus() {
        this._elementRef.nativeElement.querySelector('button').focus();
    }
    /**
     * @hidden
     * @return {?}
     */
    onTouched() { }
    /**
     * @hidden
     * @return {?}
     */
    ngAfterContentInit() {
        this._init = true;
    }
    /**
     * @hidden
     * @param {?} isDisabled
     * @return {?}
     */
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    /**
     * @hidden
     * @return {?}
     */
    ngOnDestroy() {
        this._form.deregister(this);
    }
}
Checkbox.decorators = [
    { type: Component, args: [{
                selector: 'ion-checkbox',
                template: '<div class="checkbox-icon" [class.checkbox-checked]="_checked">' +
                    '<div class="checkbox-inner"></div>' +
                    '</div>' +
                    '<button role="checkbox" ' +
                    'type="button" ' +
                    'ion-button="item-cover" ' +
                    '[id]="id" ' +
                    '[attr.aria-checked]="_checked" ' +
                    '[attr.aria-labelledby]="_labelId" ' +
                    '[attr.aria-disabled]="_disabled" ' +
                    'class="item-cover"> ' +
                    '</button>',
                host: {
                    '[class.checkbox-disabled]': '_disabled'
                },
                providers: [CHECKBOX_VALUE_ACCESSOR],
                encapsulation: ViewEncapsulation.None,
            },] },
];
/**
 * @nocollapse
 */
Checkbox.ctorParameters = () => [
    { type: Config, },
    { type: Form, },
    { type: Item, decorators: [{ type: Optional },] },
    { type: ElementRef, },
    { type: Renderer, },
    { type: ChangeDetectorRef, },
];
Checkbox.propDecorators = {
    'ionChange': [{ type: Output },],
    '_click': [{ type: HostListener, args: ['click', ['$event'],] },],
    'checked': [{ type: Input },],
    'disabled': [{ type: Input },],
};
function Checkbox_tsickle_Closure_declarations() {
    /** @type {?} */
    Checkbox.decorators;
    /**
     * @nocollapse
     * @type {?}
     */
    Checkbox.ctorParameters;
    /** @type {?} */
    Checkbox.propDecorators;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype._checked;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype._init;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype._disabled;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype._labelId;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype._fn;
    /**
     * @hidden
     * @type {?}
     */
    Checkbox.prototype.id;
    /**
     * \@output {Checkbox} Emitted when the checkbox value changes.
     * @type {?}
     */
    Checkbox.prototype.ionChange;
    /** @type {?} */
    Checkbox.prototype._form;
    /** @type {?} */
    Checkbox.prototype._item;
    /** @type {?} */
    Checkbox.prototype._cd;
}
//# sourceMappingURL=checkbox.js.map