/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, Input, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzI18nService } from 'ng-zorro-antd/i18n';
import { NzAutosizeDirective } from 'ng-zorro-antd/input';
import * as i0 from "@angular/core";
import * as i1 from "ng-zorro-antd/i18n";
import * as i2 from "@angular/common";
import * as i3 from "ng-zorro-antd/core";
import * as i4 from "ng-zorro-antd/tooltip";
import * as i5 from "ng-zorro-antd/icon";
import * as i6 from "ng-zorro-antd/input";
var _c0 = ["textarea"];
function NzTextEditComponent_button_0_Template(rf, ctx) { if (rf & 1) {
    var _r2353 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "button", 2);
    i0.ɵɵlistener("click", function NzTextEditComponent_button_0_Template_button_click_0_listener() { i0.ɵɵrestoreView(_r2353); var ctx_r2352 = i0.ɵɵnextContext(); return ctx_r2352.onClick(); });
    i0.ɵɵelement(1, "i", 3);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    var ctx_r2350 = i0.ɵɵnextContext();
    i0.ɵɵproperty("nzTitle", ctx_r2350.locale == null ? null : ctx_r2350.locale.edit);
} }
function NzTextEditComponent_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    var _r2356 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "textarea", 4, 5);
    i0.ɵɵlistener("input", function NzTextEditComponent_ng_container_1_Template_textarea_input_1_listener($event) { i0.ɵɵrestoreView(_r2356); var ctx_r2355 = i0.ɵɵnextContext(); return ctx_r2355.onInput($event); })("blur", function NzTextEditComponent_ng_container_1_Template_textarea_blur_1_listener() { i0.ɵɵrestoreView(_r2356); var ctx_r2357 = i0.ɵɵnextContext(); return ctx_r2357.confirm(); })("keydown.esc", function NzTextEditComponent_ng_container_1_Template_textarea_keydown_esc_1_listener() { i0.ɵɵrestoreView(_r2356); var ctx_r2358 = i0.ɵɵnextContext(); return ctx_r2358.onCancel(); })("keydown.enter", function NzTextEditComponent_ng_container_1_Template_textarea_keydown_enter_1_listener($event) { i0.ɵɵrestoreView(_r2356); var ctx_r2359 = i0.ɵɵnextContext(); return ctx_r2359.onEnter($event); });
    i0.ɵɵtext(3, "      ");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(4, "button", 6);
    i0.ɵɵlistener("click", function NzTextEditComponent_ng_container_1_Template_button_click_4_listener() { i0.ɵɵrestoreView(_r2356); var ctx_r2360 = i0.ɵɵnextContext(); return ctx_r2360.confirm(); });
    i0.ɵɵelement(5, "i", 7);
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} }
var NzTextEditComponent = /** @class */ (function () {
    function NzTextEditComponent(host, cdr, i18n) {
        this.host = host;
        this.cdr = cdr;
        this.i18n = i18n;
        this.editing = false;
        // tslint:disable-next-line:no-any
        this.locale = {};
        this.destroy$ = new Subject();
        this.startEditing = new EventEmitter();
        this.endEditing = new EventEmitter();
        this.nativeElement = this.host.nativeElement;
    }
    NzTextEditComponent.prototype.ngOnInit = function () {
        var _this = this;
        this.i18n.localeChange.pipe(takeUntil(this.destroy$)).subscribe(function () {
            _this.locale = _this.i18n.getLocaleData('Text');
            _this.cdr.markForCheck();
        });
    };
    NzTextEditComponent.prototype.ngOnDestroy = function () {
        this.destroy$.next();
        this.destroy$.complete();
    };
    NzTextEditComponent.prototype.onClick = function () {
        this.beforeText = this.text;
        this.currentText = this.beforeText;
        this.editing = true;
        this.startEditing.emit();
        this.focusAndSetValue();
    };
    NzTextEditComponent.prototype.confirm = function () {
        this.editing = false;
        this.endEditing.emit(this.currentText);
    };
    NzTextEditComponent.prototype.onInput = function (event) {
        var target = event.target;
        this.currentText = target.value;
    };
    NzTextEditComponent.prototype.onEnter = function (event) {
        event.stopPropagation();
        event.preventDefault();
        this.confirm();
    };
    NzTextEditComponent.prototype.onCancel = function () {
        this.currentText = this.beforeText;
        this.confirm();
    };
    NzTextEditComponent.prototype.focusAndSetValue = function () {
        var _this = this;
        setTimeout(function () {
            if (_this.textarea && _this.textarea.nativeElement) {
                _this.textarea.nativeElement.focus();
                _this.textarea.nativeElement.value = _this.currentText;
                _this.autosizeDirective.resizeToFitContent();
            }
        });
    };
    /** @nocollapse */ NzTextEditComponent.ɵfac = function NzTextEditComponent_Factory(t) { return new (t || NzTextEditComponent)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef), i0.ɵɵdirectiveInject(i1.NzI18nService)); };
    /** @nocollapse */ NzTextEditComponent.ɵcmp = i0.ɵɵdefineComponent({ type: NzTextEditComponent, selectors: [["nz-text-edit"]], viewQuery: function NzTextEditComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, true);
            i0.ɵɵviewQuery(NzAutosizeDirective, true);
        } if (rf & 2) {
            var _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.textarea = _t.first);
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.autosizeDirective = _t.first);
        } }, inputs: { text: "text" }, outputs: { startEditing: "startEditing", endEditing: "endEditing" }, exportAs: ["nzTextEdit"], decls: 2, vars: 2, consts: [["nz-tooltip", "", "nz-trans-button", "", "class", "ant-typography-edit", 3, "nzTitle", "click", 4, "ngIf"], [4, "ngIf"], ["nz-tooltip", "", "nz-trans-button", "", 1, "ant-typography-edit", 3, "nzTitle", "click"], ["nz-icon", "", "nzType", "edit"], ["nz-input", "", "nzAutosize", "", 3, "input", "blur", "keydown.esc", "keydown.enter"], ["textarea", ""], ["nz-trans-button", "", 1, "ant-typography-edit-content-confirm", 3, "click"], ["nz-icon", "", "nzType", "enter"]], template: function NzTextEditComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, NzTextEditComponent_button_0_Template, 2, 1, "button", 0);
            i0.ɵɵtemplate(1, NzTextEditComponent_ng_container_1_Template, 6, 0, "ng-container", 1);
        } if (rf & 2) {
            i0.ɵɵproperty("ngIf", !ctx.editing);
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", ctx.editing);
        } }, directives: [i2.NgIf, i3.NzTransButtonDirective, i4.NzTooltipDirective, i5.NzIconDirective, i6.NzInputDirective, i6.NzAutosizeDirective], encapsulation: 2, changeDetection: 0 });
    return NzTextEditComponent;
}());
export { NzTextEditComponent };
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(NzTextEditComponent, [{
        type: Component,
        args: [{
                selector: 'nz-text-edit',
                exportAs: 'nzTextEdit',
                template: "\n    <button *ngIf=\"!editing\" [nzTitle]=\"locale?.edit\" nz-tooltip nz-trans-button class=\"ant-typography-edit\" (click)=\"onClick()\">\n      <i nz-icon nzType=\"edit\"></i>\n    </button>\n    <ng-container *ngIf=\"editing\">\n      <textarea\n        #textarea\n        nz-input\n        nzAutosize\n        (input)=\"onInput($event)\"\n        (blur)=\"confirm()\"\n        (keydown.esc)=\"onCancel()\"\n        (keydown.enter)=\"onEnter($event)\"\n      >\n      </textarea>\n      <button nz-trans-button class=\"ant-typography-edit-content-confirm\" (click)=\"confirm()\">\n        <i nz-icon nzType=\"enter\"></i>\n      </button>\n    </ng-container>\n  ",
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                preserveWhitespaces: false
            }]
    }], function () { return [{ type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: i1.NzI18nService }]; }, { text: [{
            type: Input
        }], startEditing: [{
            type: Output
        }], endEditing: [{
            type: Output
        }], textarea: [{
            type: ViewChild,
            args: ['textarea', { static: false }]
        }], autosizeDirective: [{
            type: ViewChild,
            args: [NzAutosizeDirective, { static: false }]
        }] }); })();
//# sourceMappingURL=data:application/json;base64, 