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 { DomController } from '../../platform/dom-controller';
import { Form } from '../../util/form';
import { GestureController } from '../../gestures/gesture-controller';
import { Haptic } from '../../tap-click/haptic';
import { Ion } from '../ion';
import { isTrueProperty } from '../../util/util';
import { Item } from '../item/item';
import { KEY_ENTER, KEY_SPACE } from '../../platform/key';
import { Platform } from '../../platform/platform';
import { ToggleGesture } from './toggle-gesture';
export const /** @type {?} */ TOGGLE_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => Toggle),
    multi: true
};
/**
 * \@name Toggle
 * \@description
 * A toggle technically is the same thing as an HTML checkbox input,
 * except it looks different and is easier to use on a touch device.
 * Toggles can also have colors assigned to them, by adding any color
 * attribute.
 *
 * 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-toggle [(ngModel)]="pepperoni"></ion-toggle>
 *    </ion-item>
 *
 *    <ion-item>
 *      <ion-label>Sausage</ion-label>
 *      <ion-toggle [(ngModel)]="sausage" disabled="true"></ion-toggle>
 *    </ion-item>
 *
 *    <ion-item>
 *      <ion-label>Mushrooms</ion-label>
 *      <ion-toggle [(ngModel)]="mushrooms"></ion-toggle>
 *    </ion-item>
 *
 *  </ion-list>
 * ```
 *
 * \@demo /docs/v2/demos/src/toggle/
 * @see {\@link /docs/v2/components#toggle Toggle Component Docs}
 */
export class Toggle extends Ion {
    /**
     * @param {?} _form
     * @param {?} config
     * @param {?} _plt
     * @param {?} elementRef
     * @param {?} renderer
     * @param {?} _haptic
     * @param {?} _item
     * @param {?} _gestureCtrl
     * @param {?} _domCtrl
     * @param {?} _cd
     */
    constructor(_form, config, _plt, elementRef, renderer, _haptic, _item, _gestureCtrl, _domCtrl, _cd) {
        super(config, elementRef, renderer, 'toggle');
        this._form = _form;
        this._plt = _plt;
        this._haptic = _haptic;
        this._item = _item;
        this._gestureCtrl = _gestureCtrl;
        this._domCtrl = _domCtrl;
        this._cd = _cd;
        this._checked = false;
        this._init = false;
        this._disabled = false;
        this._activated = false;
        this._msPrv = 0;
        this._fn = null;
        /**
         * \@output {Toggle} Emitted when the toggle value changes.
         */
        this.ionChange = new EventEmitter();
        _form.register(this);
        if (_item) {
            this.id = 'tgl-' + _item.registerInput('toggle');
            this._labelId = 'lbl-' + _item.id;
            this._item.setElementClass('item-toggle', true);
        }
    }
    /**
     * @hidden
     * @return {?}
     */
    ngAfterContentInit() {
        this._init = true;
        this._gesture = new ToggleGesture(this._plt, this, this._gestureCtrl, this._domCtrl);
        this._gesture.listen();
    }
    /**
     * @hidden
     * @param {?} startX
     * @return {?}
     */
    _onDragStart(startX) {
        (void 0) /* assert */;
        (void 0) /* console.debug */;
        this._startX = startX;
        this._activated = true;
    }
    /**
     * @hidden
     * @param {?} currentX
     * @return {?}
     */
    _onDragMove(currentX) {
        if (!this._startX) {
            (void 0) /* assert */;
            return;
        }
        (void 0) /* console.debug */;
        if (this._checked) {
            if (currentX + 15 < this._startX) {
                this.onChange(false);
                this._haptic.selection();
                this._startX = currentX;
                this._activated = true;
            }
        }
        else if (currentX - 15 > this._startX) {
            this.onChange(true);
            this._haptic.selection();
            this._startX = currentX;
            this._activated = (currentX < this._startX + 5);
        }
    }
    /**
     * @hidden
     * @param {?} endX
     * @return {?}
     */
    _onDragEnd(endX) {
        if (!this._startX) {
            (void 0) /* assert */;
            return;
        }
        (void 0) /* console.debug */;
        if (this.checked) {
            if (this._startX + 4 > endX) {
                this.onChange(false);
                this._haptic.selection();
            }
        }
        else if (this._startX - 4 < endX) {
            this.onChange(true);
            this._haptic.selection();
        }
        this._activated = false;
        this._startX = null;
    }
    /**
     * \@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-toggle-checked', isChecked);
        }
    }
    /**
     * @hidden
     * @param {?} val
     * @return {?}
     */
    writeValue(val) {
        this._setChecked(isTrueProperty(val));
    }
    /**
     * @hidden
     * @param {?} fn
     * @return {?}
     */
    registerOnChange(fn) {
        this._fn = fn;
    }
    /**
     * @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-toggle-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._fn && this._fn(isChecked);
        this._setChecked(isChecked);
        this.onTouched();
        this._cd.detectChanges();
    }
    /**
     * @hidden
     * @return {?}
     */
    onTouched() { }
    /**
     * @hidden
     * @param {?} ev
     * @return {?}
     */
    _keyup(ev) {
        if (ev.keyCode === KEY_SPACE || ev.keyCode === KEY_ENTER) {
            (void 0) /* console.debug */;
            ev.preventDefault();
            ev.stopPropagation();
            this.onChange(!this._checked);
        }
    }
    /**
     * @hidden
     * @return {?}
     */
    initFocus() {
        this._elementRef.nativeElement.querySelector('button').focus();
    }
    /**
     * @hidden
     * @param {?} isDisabled
     * @return {?}
     */
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    /**
     * @hidden
     * @return {?}
     */
    ngOnDestroy() {
        this._form && this._form.deregister(this);
        this._gesture && this._gesture.destroy();
        this._fn = null;
    }
}
Toggle.decorators = [
    { type: Component, args: [{
                selector: 'ion-toggle',
                template: '<div class="toggle-icon" [class.toggle-checked]="_checked" [class.toggle-activated]="_activated">' +
                    '<div class="toggle-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.toggle-disabled]': '_disabled'
                },
                providers: [TOGGLE_VALUE_ACCESSOR],
                encapsulation: ViewEncapsulation.None,
            },] },
];
/**
 * @nocollapse
 */
Toggle.ctorParameters = () => [
    { type: Form, },
    { type: Config, },
    { type: Platform, },
    { type: ElementRef, },
    { type: Renderer, },
    { type: Haptic, },
    { type: Item, decorators: [{ type: Optional },] },
    { type: GestureController, },
    { type: DomController, },
    { type: ChangeDetectorRef, },
];
Toggle.propDecorators = {
    'ionChange': [{ type: Output },],
    'checked': [{ type: Input },],
    'disabled': [{ type: Input },],
    '_keyup': [{ type: HostListener, args: ['keyup', ['$event'],] },],
};
function Toggle_tsickle_Closure_declarations() {
    /** @type {?} */
    Toggle.decorators;
    /**
     * @nocollapse
     * @type {?}
     */
    Toggle.ctorParameters;
    /** @type {?} */
    Toggle.propDecorators;
    /** @type {?} */
    Toggle.prototype._checked;
    /** @type {?} */
    Toggle.prototype._init;
    /** @type {?} */
    Toggle.prototype._disabled;
    /** @type {?} */
    Toggle.prototype._labelId;
    /** @type {?} */
    Toggle.prototype._activated;
    /** @type {?} */
    Toggle.prototype._startX;
    /** @type {?} */
    Toggle.prototype._msPrv;
    /** @type {?} */
    Toggle.prototype._fn;
    /** @type {?} */
    Toggle.prototype._gesture;
    /**
     * @hidden
     * @type {?}
     */
    Toggle.prototype.id;
    /**
     * \@output {Toggle} Emitted when the toggle value changes.
     * @type {?}
     */
    Toggle.prototype.ionChange;
    /** @type {?} */
    Toggle.prototype._form;
    /** @type {?} */
    Toggle.prototype._plt;
    /** @type {?} */
    Toggle.prototype._haptic;
    /** @type {?} */
    Toggle.prototype._item;
    /** @type {?} */
    Toggle.prototype._gestureCtrl;
    /** @type {?} */
    Toggle.prototype._domCtrl;
    /** @type {?} */
    Toggle.prototype._cd;
}
//# sourceMappingURL=toggle.js.map