import { ContentChildren, Directive, ElementRef, EventEmitter, Input, Output, Optional, Renderer } from '@angular/core';
import { NgControl } from '@angular/forms';
import { Config } from '../../config/config';
import { Ion } from '../ion';
import { isPresent, isTrueProperty } from '../../util/util';
import { SegmentButton } from './segment-button';
/**
 * \@name Segment
 * \@description
 * A Segment is a group of buttons, sometimes known as Segmented Controls, that allow the user to interact with a compact group of a number of controls.
 * Segments provide functionality similar to tabs, selecting one will unselect all others. You should use a tab bar instead of a segmented control when you want to let the user move back and forth between distinct pages in your app.
 * You could use Angular 2's `ngModel` or `FormBuilder` API. For an overview on how `FormBuilder` works, checkout [Angular 2 Forms](http://learnangular2.com/forms/), or [Angular FormBuilder](https://angular.io/docs/ts/latest/api/forms/index/FormBuilder-class.html)
 *
 *
 * ```html
 * <!-- Segment in a header -->
 * <ion-header>
 *   <ion-toolbar>
 *     <ion-segment [(ngModel)]="icons" color="secondary">
 *       <ion-segment-button value="camera">
 *         <ion-icon name="camera"></ion-icon>
 *       </ion-segment-button>
 *       <ion-segment-button value="bookmark">
 *         <ion-icon name="bookmark"></ion-icon>
 *       </ion-segment-button>
 *     </ion-segment>
 *   </ion-toolbar>
 * </ion-header>
 *
 * <ion-content>
 *   <!-- Segment in content -->
 *   <ion-segment [(ngModel)]="relationship" color="primary">
 *     <ion-segment-button value="friends" (ionSelect)="selectedFriends()">
 *       Friends
 *     </ion-segment-button>
 *     <ion-segment-button value="enemies" (ionSelect)="selectedEnemies()">
 *       Enemies
 *     </ion-segment-button>
 *   </ion-segment>
 *
 *   <!-- Segment in a form -->
 *   <form [formGroup]="myForm">
 *     <ion-segment formControlName="mapStyle" color="danger">
 *       <ion-segment-button value="standard">
 *         Standard
 *       </ion-segment-button>
 *       <ion-segment-button value="hybrid">
 *         Hybrid
 *       </ion-segment-button>
 *       <ion-segment-button value="sat">
 *         Satellite
 *       </ion-segment-button>
 *     </ion-segment>
 *   </form>
 * </ion-content>
 * ```
 *
 *
 * \@demo /docs/v2/demos/src/segment/
 * @see {\@link /docs/v2/components#segment Segment Component Docs}
 * @see [Angular 2 Forms](http://learnangular2.com/forms/)
 */
export class Segment extends Ion {
    /**
     * @param {?} config
     * @param {?} elementRef
     * @param {?} renderer
     * @param {?} ngControl
     */
    constructor(config, elementRef, renderer, ngControl) {
        super(config, elementRef, renderer, 'segment');
        this._disabled = false;
        /**
         * \@output {Any} Emitted when a segment button has been changed.
         */
        this.ionChange = new EventEmitter();
        /**
         * @hidden
         */
        this.onChange = (_) => { };
        /**
         * @hidden
         */
        this.onTouched = (_) => { };
        if (ngControl) {
            ngControl.valueAccessor = this;
        }
    }
    /**
     * \@input {boolean} If true, the user cannot interact with any of the buttons in the segment.
     * @return {?}
     */
    get disabled() {
        return this._disabled;
    }
    /**
     * @param {?} val
     * @return {?}
     */
    set disabled(val) {
        this._disabled = isTrueProperty(val);
        if (this._buttons) {
            this._buttons.forEach(button => {
                button._setElementClass('segment-button-disabled', this._disabled);
            });
        }
    }
    /**
     * @hidden
     * Write a new value to the element.
     * @param {?} value
     * @return {?}
     */
    writeValue(value) {
        this.value = isPresent(value) ? value : '';
        if (this._buttons) {
            let /** @type {?} */ buttons = this._buttons.toArray();
            for (let /** @type {?} */ button of buttons) {
                button.isActive = (button.value === this.value);
            }
        }
    }
    /**
     * @hidden
     * @return {?}
     */
    ngAfterViewInit() {
        this._buttons.forEach(button => {
            button.ionSelect.subscribe((selectedButton) => {
                this.writeValue(selectedButton.value);
                this.onChange(selectedButton.value);
                this.ionChange.emit(selectedButton);
            });
            if (isPresent(this.value)) {
                button.isActive = (button.value === this.value);
            }
            if (isTrueProperty(this._disabled)) {
                button._setElementClass('segment-button-disabled', this._disabled);
            }
        });
    }
    /**
     * @hidden
     * Set the function to be called when the control receives a change event.
     * @param {?} fn
     * @return {?}
     */
    registerOnChange(fn) { this.onChange = fn; }
    /**
     * @hidden
     * Set the function to be called when the control receives a touch event.
     * @param {?} fn
     * @return {?}
     */
    registerOnTouched(fn) { this.onTouched = fn; }
}
Segment.decorators = [
    { type: Directive, args: [{
                selector: 'ion-segment'
            },] },
];
/**
 * @nocollapse
 */
Segment.ctorParameters = () => [
    { type: Config, },
    { type: ElementRef, },
    { type: Renderer, },
    { type: NgControl, decorators: [{ type: Optional },] },
];
Segment.propDecorators = {
    'ionChange': [{ type: Output },],
    '_buttons': [{ type: ContentChildren, args: [SegmentButton,] },],
    'disabled': [{ type: Input },],
};
function Segment_tsickle_Closure_declarations() {
    /** @type {?} */
    Segment.decorators;
    /**
     * @nocollapse
     * @type {?}
     */
    Segment.ctorParameters;
    /** @type {?} */
    Segment.propDecorators;
    /** @type {?} */
    Segment.prototype._disabled;
    /**
     * @hidden
     * @type {?}
     */
    Segment.prototype.value;
    /**
     * \@output {Any} Emitted when a segment button has been changed.
     * @type {?}
     */
    Segment.prototype.ionChange;
    /**
     * @hidden
     * @type {?}
     */
    Segment.prototype._buttons;
    /**
     * @hidden
     * @type {?}
     */
    Segment.prototype.onChange;
    /**
     * @hidden
     * @type {?}
     */
    Segment.prototype.onTouched;
}
//# sourceMappingURL=segment.js.map