import { __decorate } from 'tslib';
import { Directive, InjectionToken, Renderer2, ElementRef, Inject, Input, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, NgZone, Optional, ViewChild, ContentChild, ContentChildren, HostListener, Self, HostBinding, NgModule } from '@angular/core';
import { LyTheme2, StyleRenderer, st2c, keyframesUniqueId, StyleCollection, LY_COMMON_STYLES, toBoolean, Style, LyCommonModule } from '@alyle/ui';
import { Subject, merge } from 'rxjs';
import { NgControl, NgForm, FormGroupDirective } from '@angular/forms';
import { Platform } from '@angular/cdk/platform';
import { takeUntil, take } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
import { ObserversModule } from '@angular/cdk/observers';

class LyLabel {
}
LyLabel.decorators = [
    { type: Directive, args: [{
                selector: 'ly-field > ly-label'
            },] }
];

class LyPlaceholder {
}
LyPlaceholder.decorators = [
    { type: Directive, args: [{
                selector: 'ly-field > ly-placeholder'
            },] }
];

/**
 * For internal use only.
 * @docs-private
 */
const LY_FIELD_STYLES_TOKEN = new InjectionToken('LY_FIELD_STYLES_TOKEN');

/** LyHint */
const STYLE_PRIORITY$2 = -2;
/** Hint text to be shown underneath the field. */
class LyHint {
    constructor(_renderer, _el, _theme, styles) {
        this._renderer = _renderer;
        this._el = _el;
        this._theme = _theme;
        this.classes = this._theme.addStyleSheet(styles, STYLE_PRIORITY$2);
        _renderer.addClass(_el.nativeElement, this.classes.hint);
    }
    set align(val) {
        if (val) {
            if (val === 'after') {
                this._renderer.addClass(this._el.nativeElement, this.classes.hintAfter);
                this._alignClass = this.classes.hintAfter;
            }
            else {
                this._renderer.addClass(this._el.nativeElement, this.classes.hintBefore);
                this._alignClass = this.classes.hintBefore;
            }
        }
        else if (this._alignClass) {
            this._renderer.removeClass(this._el.nativeElement, this._alignClass);
            this._alignClass = undefined;
        }
        this._align = val;
    }
    get align() {
        return this._align;
    }
}
LyHint.decorators = [
    { type: Directive, args: [{
                selector: 'ly-field > ly-hint'
            },] }
];
LyHint.ctorParameters = () => [
    { type: Renderer2 },
    { type: ElementRef },
    { type: LyTheme2 },
    { type: undefined, decorators: [{ type: Inject, args: [LY_FIELD_STYLES_TOKEN,] }] }
];
LyHint.propDecorators = {
    align: [{ type: Input }]
};

/** Prefix to be placed the before of the field. */
class LyPrefix {
}
LyPrefix.decorators = [
    { type: Directive, args: [{
                selector: '[lyPrefix]',
            },] }
];

/** Suffix to be placed the after of the field. */
class LySuffix {
}
LySuffix.decorators = [
    { type: Directive, args: [{
                selector: '[lySuffix]',
            },] }
];

const STYLE_PRIORITY$1 = -2;
class LyError {
    constructor(renderer, el, theme, styles) {
        const className = theme.addStyleSheet(styles, STYLE_PRIORITY$1).error;
        renderer.addClass(el.nativeElement, className);
    }
}
LyError.decorators = [
    { type: Directive, args: [{
                selector: 'ly-error'
            },] }
];
LyError.ctorParameters = () => [
    { type: Renderer2 },
    { type: ElementRef },
    { type: LyTheme2 },
    { type: undefined, decorators: [{ type: Inject, args: [LY_FIELD_STYLES_TOKEN,] }] }
];

/** An interface which allows a control to work inside of a `LyField`. */
class LyFieldControlBase {
}

/**
 * Only show when input is not focused, and hides input`.
 * You can use it to display formatted values.
 */
class LyDisplayWith {
    constructor(sRenderer, styles) {
        this.sRenderer = sRenderer;
        sRenderer.renderSheet(styles, 'displayWith');
    }
}
LyDisplayWith.decorators = [
    { type: Directive, args: [{
                selector: 'ly-display-with',
                providers: [
                    StyleRenderer
                ]
            },] }
];
LyDisplayWith.ctorParameters = () => [
    { type: StyleRenderer },
    { type: undefined, decorators: [{ type: Inject, args: [LY_FIELD_STYLES_TOKEN,] }] }
];

const LY_FIELD_DEFAULT_OPTIONS = new InjectionToken('LY_FIELD_DEFAULT_OPTIONS');
/** LyField */
const STYLE_PRIORITY = -2;
const DEFAULT_APPEARANCE = 'standard';
const DEFAULT_WITH_COLOR = 'primary';
const DEFAULT_FLOATING_LABEL_SIZE = 0.75;
const inputText = [
    'text',
    'number',
    'password',
    'search',
    'tel',
    'url'
];
const STYLE_SELECT_ARROW = (className) => `${className}::after{position:absolute;content:'';width:0;height:0;border-left:0.3125em solid transparent;border-right:0.3125em solid transparent;border-top:0.3125em solid;top:50%;{after}:0;margin-top:-0.15625em;pointer-events:none;}`;
const MIXIN_CDK_TEXTAREA_AUTOSIZE_MEASURING_BASE = (className) => `${className}{padding:2px 0 !important;box-sizing:content-box !important;}`;
const ɵ0$1 = MIXIN_CDK_TEXTAREA_AUTOSIZE_MEASURING_BASE;
const STYLE_AUTOSIZE = (className) => `${className} textarea.cdk-textarea-autosize{resize:none;}${st2c((MIXIN_CDK_TEXTAREA_AUTOSIZE_MEASURING_BASE), `${className} textarea.cdk-textarea-autosize-measuring`)}${className} textarea.cdk-textarea-autosize-measuring{height:auto !important;overflow:hidden !important;}${st2c((MIXIN_CDK_TEXTAREA_AUTOSIZE_MEASURING_BASE), `${className} textarea.cdk-textarea-autosize-measuring-firefox`)}${className} textarea.cdk-textarea-autosize-measuring-firefox{height:0 !important;}`;
const ɵ1 = STYLE_AUTOSIZE;
const STYLES = (theme, ref) => {
    const classes = ref.selectorsOf(STYLES);
    const { before, after } = theme;
    const shake = keyframesUniqueId.next();
    return {
        $priority: STYLE_PRIORITY,
        $global: (className) => `${st2c((STYLE_AUTOSIZE), `${className}`)}@keyframes ${shake}{${className} 0%{margin-${before}:0;}${className} 40%{margin-${before}:2px;}${className} 50%{margin-${before}:-2px;}${className} 70%{margin-${before}:2px;}${className} 100%{margin-${before}:0;}}`,
        root: () => (className) => `${className}{display:inline-block;position:relative;line-height:1.125;}${st2c(((theme.field
            && theme.field.root
            && (theme.field.root instanceof StyleCollection
                ? theme.field.root.setTransformer(fn => fn(classes))
                : theme.field.root(classes)))), `${className}`)}${className} ${classes.hint},${className} ${classes.error}{display:block;}${className} ${classes.label} ly-icon,${className} ${classes.placeholder} ly-icon{font-size:inherit;}${className} ${classes.prefix},${className} ${classes.suffix}{position:relative;white-space:nowrap;flex:none;}`,
        animations: () => (className) => `${className} ${classes.labelSpan}{transition:${theme.animations.curves.deceleration} .${theme.animations.durations.complex}s;}${className} ${classes.label}{transition:${theme.animations.curves.deceleration} .${theme.animations.durations.complex}s;}`,
        container: (className) => `${className}{height:100%;display:flex;align-items:baseline;position:relative;-webkit-tap-highlight-color:transparent;box-sizing:border-box;}${st2c((LY_COMMON_STYLES.fill), `${className}:after`)}${className}:after{content:'';pointer-events:none;}`,
        fieldset: (className) => `${st2c((LY_COMMON_STYLES.fill), `${className}`)}${className}{margin:0;border-style:solid;border-width:0;}`,
        fieldsetSpan: (className) => `${className}{padding:0;height:2px;}`,
        prefix: (className) => `${className}{max-height:2em;}`,
        infix: (className) => `${className}{display:inline-flex;position:relative;min-width:0;width:180px;flex:auto;}`,
        suffix: (className) => `${className}{max-height:2em;}`,
        labelContainer: (className) => `${st2c((LY_COMMON_STYLES.fill), `${className}`)}${className}{pointer-events:none;display:flex;width:100%;}`,
        labelSpacingStart: null,
        labelCenter: (className) => `${className}{display:flex;max-width:100%;}`,
        labelSpacingEnd: (className) => `${className}{flex:1;}`,
        label: (className) => `${st2c((LY_COMMON_STYLES.fill), `${className}`)}${className}{margin:0;border:none;pointer-events:none;overflow:hidden;width:100%;height:100%;}`,
        labelSpan: (className) => `${className}{white-space:nowrap;text-overflow:ellipsis;overflow:hidden;display:block;width:100%;height:100%;transform-origin:${before} 0;}`,
        isFloatingLabel: null,
        floatingLabel: null,
        placeholder: (className) => `${st2c((LY_COMMON_STYLES.fill), `${className}`)}${className}{pointer-events:none;white-space:nowrap;text-overflow:ellipsis;overflow:hidden;}`,
        focused: null,
        inputNative: (className) => `${className}{padding:0;outline:none;border:none;background-color:transparent;color:inherit;font:inherit;width:100%;}textarea${className}{padding:2px 0;margin:-2px 0;resize:vertical;overflow:auto;}select${className}{-moz-appearance:none;-webkit-appearance:none;position:relative;background-color:transparent;display:inline-flex;box-sizing:border-box;padding-${after}:1em;}select${className} option:not([disabled]){color:initial;}select${className} optgroup:not([disabled]){color:initial;}select${className}::-ms-expand{display:none;}select${className}::-moz-focus-inner{border:0;}select${className}:not(:disabled){cursor:pointer;}select${className}::-ms-value{color:inherit;background:0 0;}`,
        /** Is used to hide the input when `displayWith` is shown */
        _hiddenInput: (className) => `${className}{color:transparent;}`,
        displayWith: (className) => `${st2c((LY_COMMON_STYLES.fill), `${className}`)}${className}{white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:100%;pointer-events:none;}`,
        hintContainer: (className) => `${className}{min-height:1.25em;font-size:0.75em;margin-top:.25em;}${className} > div{display:flex;max-width:100%;overflow:hidden;justify-content:space-between;}`,
        disabled: () => (className) => `${className},${className} ${classes.label},${className} ${classes.container}:after{color:${theme.disabled.contrast};cursor:default;}`,
        hint: null,
        error: null,
        errorState: () => (className) => `${className} ${classes.label},${className} ${classes.hintContainer},${className}${classes.selectArrow} ${classes.infix}:after{color:${theme.warn.default}!important;}${className} ${classes.fieldset},${className} ${classes.container}:after{border-color:${theme.warn.default}!important;}${className} ${classes.inputNative}{caret-color:${theme.warn.default}!important;}${className} ${classes.hintContainer} ly-hint:not(${classes.hintAfter}){display:none;}${className} ${classes.labelSpan}{animation:${shake} ${theme.animations.durations.complex}ms ${theme.animations.curves.deceleration};}${className} ${classes.inputNative}::selection,${className} ${classes.inputNative}::-moz-selection{background-color:${theme.warn.default} !important;color:${theme.warn.contrast} !important;}`,
        hintAfter: (className) => `${className}{margin-${before}:auto;}`,
        hintBefore: (className) => `${className}{margin-${after}:auto;}`,
        selectArrow: () => (className) => `${className} ${classes.infix}::after{position:absolute;content:'';width:0;height:0;border-left:0.3125em solid transparent;border-right:0.3125em solid transparent;border-top:0.3125em solid;top:0;${after}:0;pointer-events:none;}`
    };
};
const ɵ2 = (val, media) => ({ breakpoints }, ref) => {
    const classes = ref.selectorsOf(STYLES);
    return (className) => `@media ${(media && breakpoints[media]) || 'all'}{${className} ${classes.floatingLabel} ${classes.labelSpan}{transform:scale(${val});width:${Math.round(100 / 0.75)}%;}}`;
}, ɵ3 = val => (theme, ref) => {
    const classes = ref.selectorsOf(STYLES);
    const color = theme.colorOf(val);
    const contrast = theme.colorOf(`${val}:contrast`);
    return (className) => `${className}${classes.focused} ${classes.container}:after,${className}${classes.focused}${classes.selectArrow} ${classes.infix}:after{color:${color};}${className}${classes.focused} ${classes.fieldset}{border-color:${color};}${className}${classes.focused} ${classes.label}{color:${color};}${className} ${classes.inputNative}{caret-color:${color};}${className} ${classes.inputNative}::selection{background-color:${color};color:${contrast};}${className} ${classes.inputNative}::-moz-selection{background-color:${color};color:${contrast};}`;
}, ɵ4 = val => (theme, ref) => {
    var _a;
    const classes = ref.selectorsOf(STYLES);
    if ((_a = theme.field) === null || _a === void 0 ? void 0 : _a.appearance) {
        const appearance = theme.field.appearance[val];
        if (appearance) {
            return appearance instanceof StyleCollection
                ? appearance.setTransformer((_) => _(classes)).css
                : appearance(classes);
        }
    }
    throw new Error(`[${val}] not found in theme.field.appearance`);
};
/**
 * @dynamic
 */
class LyField {
    constructor(_renderer, _el, _theme, _cd, _ngZone, sRenderer, _platform, _defaults) {
        this._renderer = _renderer;
        this._el = _el;
        this._theme = _theme;
        this._cd = _cd;
        this._ngZone = _ngZone;
        this.sRenderer = sRenderer;
        this._platform = _platform;
        this._defaults = _defaults;
        /**
         * styles
         * @docs-private
         */
        this.classes = this._theme.renderStyleSheet(STYLES);
        this._destroyed = new Subject();
        _renderer.addClass(_el.nativeElement, this.classes.root);
    }
    get _control() {
        // Support both Ivy and ViewEngine.
        return this._controlNonStatic || this._controlStatic;
    }
    get errorState() {
        return this._control ? this._control.errorState : false;
    }
    get displayWithStatus() {
        return !!(this._displayWithChild
            && this._control
            && !this._control.empty
            && !this._control.focused
            && !this._control.errorState);
    }
    set fullWidth(val) {
        const newVal = toBoolean(val);
        if (newVal) {
            this._fullWidthClass = this._theme.addStyle(`fullWidth`, {
                display: 'block',
                width: '100%'
            }, this._getHostElement(), this._fullWidthClass, STYLE_PRIORITY);
        }
        else if (this._fullWidthClass) {
            this._renderer.removeClass(this._getHostElement(), this._fullWidthClass);
            this._fullWidthClass = undefined;
        }
        this._fullWidth = newVal;
    }
    get fullWidth() {
        return this._fullWidth;
    }
    /** Whether the label is floating. */
    set floatingLabel(val) {
        this._floatingLabel = toBoolean(val);
        this._updateFloatingLabel();
    }
    get floatingLabel() {
        return this._floatingLabel;
    }
    /** The field appearance style. */
    set appearance(val) {
        if (val === 'outlined') {
            this._updateFielsetSpanOnStable = true;
        }
    }
    onFocus() {
        this._el.nativeElement.focus();
    }
    ngOnInit() {
        var _a, _b, _c, _d, _f, _g;
        if (!this.color) {
            this.color = DEFAULT_WITH_COLOR;
        }
        if (this.floatingLabelSize == null) {
            this.floatingLabelSize = (((_a = this._defaults) === null || _a === void 0 ? void 0 : _a.floatingLabelSize) != null)
                ? (_b = this._defaults) === null || _b === void 0 ? void 0 : _b.floatingLabelSize : DEFAULT_FLOATING_LABEL_SIZE;
        }
        if (!this.appearance) {
            this.appearance = ((_c = this._defaults) === null || _c === void 0 ? void 0 : _c.appearance) ? (_d = this._defaults) === null || _d === void 0 ? void 0 : _d.appearance : DEFAULT_APPEARANCE;
        }
        if (this.persistentHint == null) {
            this.persistentHint = (((_f = this._defaults) === null || _f === void 0 ? void 0 : _f.persistentHint) != null) ? this._defaults.persistentHint : false;
        }
        if (this.floatingLabel == null) {
            this.persistentHint = (((_g = this._defaults) === null || _g === void 0 ? void 0 : _g.floatingLabel) != null) ? this._defaults.floatingLabel : false;
        }
    }
    ngAfterContentInit() {
        this._control.stateChanges.subscribe(() => {
            this._updateFloatingLabel();
            this._updateDisplayWith();
            this._markForCheck();
        });
        const ngControl = this._control.ngControl;
        // Run change detection if the value changes.
        if (ngControl && ngControl.valueChanges) {
            ngControl.valueChanges.pipe(takeUntil(this._destroyed)).subscribe(() => {
                this._updateFloatingLabel();
                this._updateDisplayWith();
                this._markForCheck();
            });
        }
        this._ngZone.runOutsideAngular(() => {
            this._ngZone.onStable.pipe(takeUntil(this._destroyed)).subscribe(() => {
                if (this._updateFielsetSpanOnStable) {
                    this._updateFielsetSpan();
                }
            });
            this._ngZone.onStable.pipe(take(1), takeUntil(this._destroyed)).subscribe(() => {
                this._updateDisplayWith();
                this._renderer.addClass(this._el.nativeElement, this.classes.animations);
            });
        });
        merge(this._prefixChildren.changes, this._suffixChildren.changes).subscribe(() => {
            this._updateFielsetSpanOnStable = true;
            this._markForCheck();
        });
    }
    ngAfterViewInit() {
        this._updateFielsetSpan();
        this._updateFloatingLabel();
    }
    ngOnDestroy() {
        this._destroyed.next();
        this._destroyed.complete();
    }
    _updateFielsetSpan() {
        if (!this._platform.isBrowser) {
            return;
        }
        if (this.floatingLabelSize == null) {
            return;
        }
        if (this.appearance !== 'outlined') {
            return;
        }
        const label = this._isLabel() ? this._labelSpan.nativeElement : null;
        const labelFirstChild = this._isLabel()
            ? this._labelSpan.nativeElement.firstElementChild
            : null;
        if (!label) {
            return;
        }
        const before = this._theme.variables.before;
        const fieldsetLegend = this._getHostElement().querySelector('legend');
        if (!fieldsetLegend) {
            this._updateFielsetSpanOnStable = true;
            return;
        }
        const labelRect = label.getBoundingClientRect();
        const container = this._container.nativeElement;
        const containerRect = this._container.nativeElement.getBoundingClientRect();
        let { width } = labelFirstChild.getBoundingClientRect();
        const percent = containerRect.width / container.offsetWidth;
        const labelPercent = labelRect.width / label.offsetWidth;
        let beforeMargin = Math.abs((containerRect[before] - labelRect[before]) / percent) - 12;
        width /= labelPercent;
        width *= this.floatingLabelSize;
        // add 6px of space
        width += 6;
        width = width > (label.parentElement.offsetWidth)
            ? (label.parentElement.offsetWidth)
            : width;
        width = Math.round(width);
        beforeMargin = Math.round(beforeMargin);
        fieldsetLegend.style[`margin-right`] = ``;
        fieldsetLegend.style[`margin-left`] = ``;
        fieldsetLegend.style[`margin-${before}`] = `${beforeMargin}px`;
        this._updateFielsetSpanOnStable = false;
        this._fielsetSpanClass = this._theme.addStyle(`style.fieldsetSpanFocused:${width}`, {
            [`&.${this.classes.isFloatingLabel} .${this.classes.fieldsetSpan}`]: { width: `${width}px` }
        }, this._el.nativeElement, this._fielsetSpanClass, STYLE_PRIORITY);
    }
    /** @ignore */
    _isLabel() {
        if (this._control && this._control.placeholder && !this._labelChild) {
            return true;
        }
        else if (this._labelChild || this._placeholderChild) {
            return true;
        }
        return false;
    }
    /** @ignore */
    _isPlaceholder() {
        if ((this._labelChild && this._control && this._control.placeholder) || (this._labelChild && this._placeholderChild)) {
            return true;
        }
        return false;
    }
    /** @ignore */
    _isEmpty() {
        const val = this._control ? this._control.value : null;
        return val === '' || val === null || val === undefined;
    }
    _updateFloatingLabel() {
        if (this._labelContainer2) {
            const isFloating = this._control.floatingLabel || this.floatingLabel;
            if (this._isFloating !== isFloating) {
                this._isFloating = isFloating;
                if (isFloating) {
                    this._renderer.addClass(this._labelContainer2.nativeElement, this.classes.floatingLabel);
                    this._renderer.addClass(this._el.nativeElement, this.classes.isFloatingLabel);
                }
                else {
                    this._renderer.removeClass(this._labelContainer2.nativeElement, this.classes.floatingLabel);
                    this._renderer.removeClass(this._el.nativeElement, this.classes.isFloatingLabel);
                }
            }
        }
        if (this._control) {
            if (this._control.focused) {
                this._renderer.addClass(this._el.nativeElement, this.classes.focused);
            }
            else {
                this._renderer.removeClass(this._el.nativeElement, this.classes.focused);
            }
        }
    }
    _updateDisplayWith() {
        if (this._control) {
            this._control.sRenderer.toggleClass(this.classes._hiddenInput, this.displayWithStatus);
        }
    }
    _markForCheck() {
        this._cd.markForCheck();
    }
    _getHostElement() {
        return this._el.nativeElement;
    }
}
LyField.decorators = [
    { type: Component, args: [{
                selector: 'ly-field',
                exportAs: 'lyFormField',
                template: "<div #_container\n  [className]=\"classes.container\"\n  (click)=\"_control && _control.onContainerClick && _control.onContainerClick($event)\">\n  <fieldset\n    *ngIf=\"appearance === 'outlined'\"\n    [className]=\"classes.fieldset\"><legend [className]=\"classes.fieldsetSpan\"></legend></fieldset>\n  <div [className]=\"classes.prefix\" *ngIf=\"_prefixChildren.length\" #_prefixContainer>\n    <ng-content select=\"[lyPrefix]\"></ng-content>\n  </div>\n  <div [className]=\"classes.infix\">\n    <ng-container\n      *ngIf=\"displayWithStatus\"\n    >\n      <ng-content select=\"ly-display-with\"></ng-content>\n    </ng-container>\n    <ng-content></ng-content>\n    <span [className]=\"classes.label\" *ngIf=\"_isLabel()\" #_labelContainer2>\n      <span #_labelSpan [className]=\"classes.labelSpan\"\n        (cdkObserveContent)=\"_updateFielsetSpan()\"\n        [cdkObserveContentDisabled]=\"appearance != 'outlined'\"\n      >\n        <ng-container *ngTemplateOutlet=\"_labelTemplate\"></ng-container>\n      </span>\n    </span>\n    <div [className]=\"classes.placeholder\"\n      *ngIf=\"_isPlaceholder() && _control?.empty && (_control?.floatingLabel || floatingLabel)\"\n    >\n      <ng-container *ngTemplateOutlet=\"_placeholderTemplate\"></ng-container>\n    </div>\n  </div>\n  <div [className]=\"classes.suffix\" *ngIf=\"_suffixChildren.length\" #_suffixContainer>\n    <ng-content select=\"[lySuffix]\"></ng-content>\n  </div>\n</div>\n\n<div [className]=\"classes.hintContainer\">\n  <div *ngIf=\"(_hintChildren.length || _errorChildren.length) && (persistentHint || _control?.errorState || _control?.focused)\">\n    <ng-container *ngIf=\"_control?.errorState\">\n      <ng-content select=\"ly-error\"></ng-content>\n    </ng-container>\n    <ng-content select=\"ly-hint\"></ng-content>\n  </div>\n</div>\n\n<ng-template #_labelTemplate>\n  <ng-content select=\"ly-label\"></ng-content>\n  <ng-container *ngIf=\"!_labelChild\">\n    <ng-template *ngTemplateOutlet=\"_placeholderTemplate\"></ng-template>\n  </ng-container>\n</ng-template>\n\n<ng-template #_placeholderTemplate>\n  <ng-content select=\"ly-placeholder\"></ng-content>\n  <span *ngIf=\"_control?.placeholder\">{{ _control?.placeholder }}</span>\n</ng-template>\n",
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                providers: [
                    StyleRenderer,
                ]
            },] }
];
LyField.ctorParameters = () => [
    { type: Renderer2 },
    { type: ElementRef },
    { type: LyTheme2 },
    { type: ChangeDetectorRef },
    { type: NgZone },
    { type: StyleRenderer },
    { type: Platform },
    { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [LY_FIELD_DEFAULT_OPTIONS,] }] }
];
LyField.propDecorators = {
    _container: [{ type: ViewChild, args: ['_container', { static: true },] }],
    _labelContainer: [{ type: ViewChild, args: ['_labelContainer',] }],
    _labelContainer2: [{ type: ViewChild, args: ['_labelContainer2',] }],
    _labelSpan: [{ type: ViewChild, args: ['_labelSpan',] }],
    _prefixContainer: [{ type: ViewChild, args: ['_prefixContainer',] }],
    _suffixContainer: [{ type: ViewChild, args: ['_suffixContainer',] }],
    _controlNonStatic: [{ type: ContentChild, args: [LyFieldControlBase,] }],
    _controlStatic: [{ type: ContentChild, args: [LyFieldControlBase, { static: true },] }],
    _placeholderChild: [{ type: ContentChild, args: [LyPlaceholder,] }],
    _labelChild: [{ type: ContentChild, args: [LyLabel,] }],
    _displayWithChild: [{ type: ContentChild, args: [LyDisplayWith,] }],
    _hintChildren: [{ type: ContentChildren, args: [LyHint,] }],
    _prefixChildren: [{ type: ContentChildren, args: [LyPrefix,] }],
    _suffixChildren: [{ type: ContentChildren, args: [LySuffix,] }],
    _errorChildren: [{ type: ContentChildren, args: [LyError,] }],
    persistentHint: [{ type: Input }],
    floatingLabelSize: [{ type: Input }],
    fullWidth: [{ type: Input }],
    floatingLabel: [{ type: Input }],
    color: [{ type: Input }],
    appearance: [{ type: Input }],
    onFocus: [{ type: HostListener, args: ['focus',] }]
};
__decorate([
    Style(ɵ2)
], LyField.prototype, "floatingLabelSize", void 0);
__decorate([
    Style(ɵ3)
], LyField.prototype, "color", void 0);
__decorate([
    Style(ɵ4, STYLE_PRIORITY)
], LyField.prototype, "appearance", null);
class LyNativeControl {
    constructor(_theme, sRenderer, _el, _renderer, _field, 
    /** @docs-private */
    ngControl, _parentForm, _parentFormGroup) {
        this._theme = _theme;
        this.sRenderer = sRenderer;
        this._el = _el;
        this._renderer = _renderer;
        this._field = _field;
        this.ngControl = ngControl;
        this._parentForm = _parentForm;
        this._parentFormGroup = _parentFormGroup;
        this._disabled = false;
        this._required = false;
        this.stateChanges = new Subject();
        this._form = this._parentForm || this._parentFormGroup;
        this._focused = false;
        this.errorState = false;
    }
    _onInput() {
        this.stateChanges.next();
    }
    _onBlur() {
        if (this._focused !== false) {
            this._focused = false;
            this.stateChanges.next();
        }
    }
    _onFocus() {
        if (this._focused !== true) {
            this._focused = true;
            this.stateChanges.next();
        }
    }
    /** @ignore */
    set value(val) {
        if (val !== this.value) {
            this._getHostElement().value = val;
            this.stateChanges.next();
        }
    }
    get value() {
        return this._getHostElement().value;
    }
    /** Whether the input is disabled. */
    set disabled(val) {
        if (val !== this._disabled) {
            this._disabled = toBoolean(val);
            if (this._field) {
                if (!val && this._hasDisabledClass) {
                    this._renderer.removeClass(this._field._getHostElement(), this._field.classes.disabled);
                    if (this._cursorClass) {
                        this._renderer.addClass(this._field._getHostElement(), this._cursorClass);
                    }
                    this._hasDisabledClass = undefined;
                }
                else if (val) {
                    this._renderer.addClass(this._field._getHostElement(), this._field.classes.disabled);
                    if (this._cursorClass) {
                        this._renderer.removeClass(this._field._getHostElement(), this._cursorClass);
                    }
                    this._hasDisabledClass = true;
                }
            }
        }
    }
    get disabled() {
        if (this.ngControl && this.ngControl.disabled !== null) {
            return this.ngControl.disabled;
        }
        return this._disabled;
    }
    set required(value) {
        this._required = toBoolean(value);
    }
    get required() { return this._required; }
    set placeholder(val) {
        this._placeholder = val;
    }
    get placeholder() { return this._placeholder; }
    get focused() {
        return this._focused;
    }
    get empty() {
        const val = this.value;
        return val === '' || val == null;
    }
    get floatingLabel() {
        return this.focused || !this.empty || (this._isSelectInput ? this._hasLabelSelectionOption() : false);
    }
    ngOnInit() {
        this._renderer.setAttribute(this._getHostElement(), 'placeholder', '­');
        const { nativeElement } = this._el;
        if (nativeElement.nodeName.toLowerCase() === 'select') {
            this._isSelectInput = true;
        }
        // apply class {selectArrow} to `<select> not multiple`
        if (this._field && nativeElement.type === 'select-one') {
            this._renderer.addClass(this._field._getHostElement(), this._field.classes.selectArrow);
        }
        // apply style cursor only for input of type text
        if (nativeElement instanceof HTMLTextAreaElement ||
            inputText.some(type => type === nativeElement.type)) {
            this._cursorClass = this._theme.addSimpleStyle('lyField.text', {
                '& {infix}': {
                    cursor: 'text'
                }
            }, STYLE_PRIORITY, STYLES);
        }
        if (this._isSelectInput) {
            this._cursorClass = this._theme.addSimpleStyle('lyField.select', {
                '& {infix}': {
                    cursor: 'pointer'
                }
            }, STYLE_PRIORITY, STYLES);
        }
        if (this._cursorClass) {
            this._renderer.addClass(this._field._getHostElement(), this._cursorClass);
        }
        // apply default styles
        this._renderer.addClass(nativeElement, this._field.classes.inputNative);
        const ngControl = this.ngControl;
        // update styles on disabled
        if (ngControl && ngControl.statusChanges) {
            ngControl.statusChanges.subscribe(() => {
                this.disabled = !!ngControl.disabled;
            });
        }
    }
    ngDoCheck() {
        if (this._field._control) {
            const oldVal = this.errorState;
            const newVal = !!(this.ngControl && this.ngControl.invalid && (this.ngControl.touched || (this._form && this._form.submitted)));
            if (newVal !== oldVal) {
                this.errorState = newVal;
                if (this._field) {
                    const errorClass = this._field.classes.errorState;
                    if (newVal) {
                        this._renderer.addClass(this._field._getHostElement(), errorClass);
                        this._errorClass = errorClass;
                    }
                    else if (this._errorClass) {
                        this._renderer.removeClass(this._field._getHostElement(), errorClass);
                        this._errorClass = undefined;
                    }
                    this.stateChanges.next();
                }
            }
        }
    }
    ngOnDestroy() {
        this.stateChanges.complete();
    }
    /** @docs-private */
    onContainerClick(_e) {
        this._getHostElement().focus();
    }
    /** Focuses the input. */
    focus() { this._getHostElement().focus(); }
    _getHostElement() {
        return this._el.nativeElement;
    }
    _hasLabelSelectionOption() {
        const el = this._getHostElement();
        const option = el.selectedOptions ? el.selectedOptions.item(0) : null;
        return option ? !!option.label : false;
    }
}
LyNativeControl.decorators = [
    { type: Directive, args: [{
                selector: 'input[lyInput], textarea[lyInput], input[lyNativeControl], textarea[lyNativeControl], select[lyNativeControl]',
                exportAs: 'LyNativeControl',
                providers: [
                    StyleRenderer,
                    { provide: LyFieldControlBase, useExisting: LyNativeControl }
                ]
            },] }
];
LyNativeControl.ctorParameters = () => [
    { type: LyTheme2 },
    { type: StyleRenderer },
    { type: ElementRef },
    { type: Renderer2 },
    { type: LyField, decorators: [{ type: Optional }] },
    { type: NgControl, decorators: [{ type: Optional }, { type: Self }] },
    { type: NgForm, decorators: [{ type: Optional }] },
    { type: FormGroupDirective, decorators: [{ type: Optional }] }
];
LyNativeControl.propDecorators = {
    _onInput: [{ type: HostListener, args: ['input',] }],
    _onBlur: [{ type: HostListener, args: ['blur',] }],
    _onFocus: [{ type: HostListener, args: ['focus',] }],
    value: [{ type: Input }],
    disabled: [{ type: HostBinding }, { type: Input }],
    required: [{ type: HostBinding }, { type: Input }],
    placeholder: [{ type: Input }]
};

const ɵ0 = STYLES;
class LyFieldModule {
}
LyFieldModule.decorators = [
    { type: NgModule, args: [{
                imports: [
                    CommonModule,
                    LyCommonModule,
                    ObserversModule
                ],
                exports: [
                    LyField,
                    LyPlaceholder,
                    LyLabel,
                    LyNativeControl,
                    LyPrefix,
                    LySuffix,
                    LyHint,
                    LyError,
                    LyDisplayWith,
                    LyCommonModule
                ],
                providers: [
                    {
                        provide: LY_FIELD_STYLES_TOKEN,
                        useValue: ɵ0
                    }
                ],
                declarations: [
                    LyField,
                    LyPlaceholder,
                    LyLabel,
                    LyNativeControl,
                    LyPrefix,
                    LySuffix,
                    LyHint,
                    LyError,
                    LyDisplayWith
                ]
            },] }
];

/**
 * Generated bundle index. Do not edit.
 */

export { LY_FIELD_DEFAULT_OPTIONS, LyField, LyFieldControlBase, LyFieldModule, LyNativeControl, STYLES, STYLE_SELECT_ARROW, ɵ1, ɵ2, ɵ3, ɵ4, LyPlaceholder as ɵa, LyLabel as ɵb, LyDisplayWith as ɵc, LY_FIELD_STYLES_TOKEN as ɵd, LyHint as ɵe, LyPrefix as ɵf, LySuffix as ɵg, LyError as ɵh };
//# sourceMappingURL=alyle-ui-field.js.map
