import * as i0 from '@angular/core';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewChild, Input, Output, HostBinding } from '@angular/core';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i2 from '@angular/forms';
import { Validators, FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BehaviorSubject, Observable } from 'rxjs';
import { isNullOrUndefined, isNumber } from '@ironsource/fusion-ui/utils';
import { InlineInputType, InputInlineComponent } from '@ironsource/fusion-ui/components/input-inline/v3';

const _c0 = ["inputInline"];
function CustomCellEditComponent_ng_container_7_div_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 5)(1, "div", 6);
    i0.ɵɵtext(2);
    i0.ɵɵpipe(3, "currency");
    i0.ɵɵelementEnd();
    i0.ɵɵtext(4, " remains");
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r2 = i0.ɵɵnextContext(2);
    i0.ɵɵclassProp("fu-no-remains", ctx_r2.remaining <= 0);
    i0.ɵɵadvance(2);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(3, 3, ctx_r2.remaining));
} }
function CustomCellEditComponent_ng_container_7_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, CustomCellEditComponent_ng_container_7_div_1_Template, 5, 5, "div", 4);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", ctx_r1.hasRemaining);
} }
class CustomCellEditComponent {
    constructor() {
        /** @internal */
        this.inputType = InlineInputType.Currency;
        /** @internal */
        this.formControl = new FormControl(null, [Validators.required, Validators.min(2)]);
        /** @internal */
        this.inputError$ = new BehaviorSubject('');
        /** @internal */
        this.isInRequest$ = new BehaviorSubject(false);
        /** @internal */
        this.editMode$ = new Observable();
        /** @internal */
        this.hasRemaining = false;
        this._remaining = 0;
        this.dataChange = new EventEmitter();
        this.errorMessages = {
            required: 'Required!',
            min: 'Minimum value: 2'
        };
        this.keyChanged = 'customCell';
        this._showInput = false;
    }
    set data(value) {
        if (!isNullOrUndefined(value)) {
            this.initInputData = value;
            this.formControl.setValue(value, { emitEvent: false });
        }
    }
    /** @internal */
    set remaining(remaining) {
        this.hasRemaining = isNumber(remaining);
        if (this.hasRemaining) {
            this._remaining = remaining;
        }
    }
    get remaining() {
        return this._remaining;
    }
    get showInput() {
        return this._showInput;
    }
    ngAfterViewInit() {
        this.editMode$ = this.inputInlineComponent.isEditMode$;
    }
    /** @internal */
    showEdit() {
        this._showInput = true;
        this.inputInlineComponent.goToEditMode(this.initInputData);
    }
    saveChanges(valuesOptions) {
        if (this.formControl.valid) {
            const newValue = Number.parseFloat(valuesOptions.newValue);
            const prevValue = valuesOptions.currentValue;
            const inlineInputComponent = this.inputInlineComponent;
            if (newValue !== prevValue) {
                this.isInRequest$.next(true);
                this.dataChange.emit({
                    newValue: newValue,
                    prevValue: prevValue,
                    cellPosition: this.cellPosition,
                    keyChanged: this.keyChanged,
                    /* Name for callback method in TableStoryHolderComponent */
                    onRequestDone: (isSuccess, error, stayInEditMode = false) => {
                        if (!isSuccess) {
                            if (stayInEditMode) {
                                inlineInputComponent.setEditMode$.next(newValue);
                            }
                            else {
                                this.inputError$.next(error && error.message);
                            }
                            this.initInputData = prevValue;
                        }
                        else {
                            this.inputError$.next('');
                            this.formControl.setValue(newValue, { emitEvent: false });
                        }
                        this.isInRequest$.next(false);
                        this._showInput = false;
                    }
                });
            }
        }
        else {
            const allErrors = this.formControl.errors || {};
            Object.keys(allErrors).forEach(errorKey => {
                this.inputError$.next(this.errorMessages[errorKey] || 'Error occur.');
            });
        }
    }
    /** @internal */
    cancelEdit() {
        this.inputError$.next('');
        this._showInput = false;
        // Temp solution - need to fix inline input behavior
        this.formControl.setValue(this.inputInlineComponent.inputControl.value, { emitEvent: false });
    }
    static { this.ɵfac = function CustomCellEditComponent_Factory(t) { return new (t || CustomCellEditComponent)(); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: CustomCellEditComponent, selectors: [["fusion-custom-cell-edit"]], viewQuery: function CustomCellEditComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.inputInlineComponent = _t.first);
        } }, hostVars: 2, hostBindings: function CustomCellEditComponent_HostBindings(rf, ctx) { if (rf & 2) {
            i0.ɵɵclassProp("fu-show-edit", ctx.showInput);
        } }, inputs: { data: "data", cellPosition: "cellPosition", remaining: "remaining" }, outputs: { dataChange: "dataChange" }, standalone: true, features: [i0.ɵɵStandaloneFeature], decls: 10, vars: 16, consts: [[1, "fu-large", "fu-align-right", 3, "type", "formControl", "loading", "error", "onSave", "onCancel"], ["inputInline", ""], [1, "fu-value", 3, "click"], [4, "ngIf"], ["class", "fu-remains", 3, "fu-no-remains", 4, "ngIf"], [1, "fu-remains"], [1, "fu-remains-value"]], template: function CustomCellEditComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵelementStart(0, "fusion-input-inline", 0, 1);
            i0.ɵɵlistener("onSave", function CustomCellEditComponent_Template_fusion_input_inline_onSave_0_listener($event) { return ctx.saveChanges($event); })("onCancel", function CustomCellEditComponent_Template_fusion_input_inline_onCancel_0_listener() { return ctx.cancelEdit(); });
            i0.ɵɵpipe(2, "async");
            i0.ɵɵpipe(3, "async");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(4, "div", 2);
            i0.ɵɵlistener("click", function CustomCellEditComponent_Template_div_click_4_listener() { return ctx.showEdit(); });
            i0.ɵɵtext(5);
            i0.ɵɵpipe(6, "currency");
            i0.ɵɵelementEnd();
            i0.ɵɵtemplate(7, CustomCellEditComponent_ng_container_7_Template, 2, 1, "ng-container", 3);
            i0.ɵɵpipe(8, "async");
            i0.ɵɵpipe(9, "async");
        } if (rf & 2) {
            i0.ɵɵproperty("type", ctx.inputType)("formControl", ctx.formControl)("loading", i0.ɵɵpipeBind1(2, 6, ctx.isInRequest$))("error", i0.ɵɵpipeBind1(3, 8, ctx.inputError$));
            i0.ɵɵadvance(5);
            i0.ɵɵtextInterpolate1(" ", ctx.formControl.value ? i0.ɵɵpipeBind1(6, 10, ctx.formControl.value) : "Unlimited", "\n");
            i0.ɵɵadvance(2);
            i0.ɵɵproperty("ngIf", ctx.formControl.value && (i0.ɵɵpipeBind1(8, 12, ctx.editMode$) === false || i0.ɵɵpipeBind1(9, 14, ctx.editMode$) === null));
        } }, dependencies: [CommonModule, i1.NgIf, i1.AsyncPipe, i1.CurrencyPipe, FormsModule, i2.NgControlStatus, ReactiveFormsModule, i2.FormControlDirective, InputInlineComponent], styles: ["@font-face{font-family:NHaasGroteskDSPro;src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.eot);src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.ttf) format(\"truetype\")}@font-face{font-family:NHaasGroteskDSPro-65Md;src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.eot);src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.ttf) format(\"truetype\")}@font-face{font-family:HelveticaNeue;src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.eot);src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.ttf) format(\"truetype\")}@font-face{font-family:HelveticaNeue-Medium;src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.eot);src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.ttf) format(\"truetype\")}.font-heading-6[_ngcontent-%COMP%], .font-heading-5[_ngcontent-%COMP%], .font-heading-4[_ngcontent-%COMP%], .font-heading-3[_ngcontent-%COMP%], .font-heading-2[_ngcontent-%COMP%], .font-heading-1[_ngcontent-%COMP%]{font-family:NHaasGroteskDSPro,sans-serif}.font-heading-1[_ngcontent-%COMP%]{font-size:60px}.font-heading-2[_ngcontent-%COMP%]{font-size:32px}.font-heading-3[_ngcontent-%COMP%]{font-size:22px}.font-heading-4[_ngcontent-%COMP%]{font-size:18px}.font-heading-5[_ngcontent-%COMP%]{font-size:15px}.font-heading-6[_ngcontent-%COMP%]{font-size:13px}.font-heading-label[_ngcontent-%COMP%]{font-size:11px;font-family:NHaasGroteskDSPro-65Md,sans-serif;letter-spacing:.25px}.font-caption[_ngcontent-%COMP%], .font-bodyXSmall[_ngcontent-%COMP%], [_nghost-%COMP%], .font-bodySmall[_ngcontent-%COMP%], .font-bodyMedium[_ngcontent-%COMP%], .font-bodyLarge[_ngcontent-%COMP%]{font-family:HelveticaNeue,helvetica,sans-serif;line-height:1.5}.font-bodyXSmallStrong[_ngcontent-%COMP%], .font-bodySmallStrong[_ngcontent-%COMP%], .font-bodyMediumStrong[_ngcontent-%COMP%], .font-bodyLargeStrong[_ngcontent-%COMP%]{font-family:HelveticaNeue-Medium,helvetica,sans-serif;line-height:1.5}.font-bodyLarge[_ngcontent-%COMP%], .font-bodyLargeStrong[_ngcontent-%COMP%]{font-size:16px}.font-bodyMedium[_ngcontent-%COMP%], .font-bodyMediumStrong[_ngcontent-%COMP%]{font-size:14px}[_nghost-%COMP%], .font-bodySmall[_ngcontent-%COMP%]{font-size:13px;letter-spacing:.24px}.font-bodySmallStrong[_ngcontent-%COMP%]{font-size:13px;letter-spacing:.24px}.font-bodyXSmall[_ngcontent-%COMP%], .font-bodyXSmallStrong[_ngcontent-%COMP%]{font-size:12px;letter-spacing:.24px}.font-caption[_ngcontent-%COMP%]{font-size:11px}[_nghost-%COMP%]{display:flex;flex-direction:column;justify-content:center;max-width:150px;height:48px;margin:8px 0;min-width:140px;border-radius:8px}[_nghost-%COMP%]   fusion-input-inline[_ngcontent-%COMP%]{display:none}[_nghost-%COMP%]   .fu-value[_ngcontent-%COMP%]{padding:4px 8px 4px 4px;display:flex;align-items:center;justify-content:flex-end;height:24px;cursor:pointer;-webkit-user-select:none;user-select:none}[_nghost-%COMP%]   .fu-remains[_ngcontent-%COMP%]{padding:0 8px;display:flex;flex-direction:row;justify-content:flex-end;gap:4px;color:var(--fu-light-900, #7B838C);-webkit-user-select:none;user-select:none;line-height:14px}[_nghost-%COMP%]   .fu-remains.fu-no-remains[_ngcontent-%COMP%]{color:var(--fu-negative-700, #DA564F)}[_nghost-%COMP%]   .fu-value[_ngcontent-%COMP%]:first-letter, [_nghost-%COMP%]   .fu-remains-value[_ngcontent-%COMP%]:first-letter{padding-right:.3em;-webkit-user-select:none;user-select:none}.fu-show-edit[_nghost-%COMP%]   fusion-input-inline[_ngcontent-%COMP%]{display:unset}.fu-show-edit[_nghost-%COMP%]   .fu-value[_ngcontent-%COMP%]{display:none}tr:hover[_nghost-%COMP%], tr:hover   [_nghost-%COMP%]{background-color:var(--fu-light-200, #EDEFF0)}tr:hover.fu-show-edit[_nghost-%COMP%], tr:hover   .fu-show-edit[_nghost-%COMP%]{background-color:transparent}"], changeDetection: 0 }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(CustomCellEditComponent, [{
        type: Component,
        args: [{ selector: 'fusion-custom-cell-edit', standalone: true, imports: [CommonModule, FormsModule, ReactiveFormsModule, InputInlineComponent], changeDetection: ChangeDetectionStrategy.OnPush, template: "<fusion-input-inline #inputInline\n                     class=\"fu-large fu-align-right\"\n                     [type]=\"inputType\"\n                     [formControl]=\"formControl\"\n                     [loading]=\"isInRequest$ | async\"\n                     [error]=\"inputError$ | async\"\n                     (onSave)=\"saveChanges($event)\"\n                     (onCancel)=\"cancelEdit()\"\n></fusion-input-inline>\n<div class=\"fu-value\" (click)=\"showEdit()\">\n    {{formControl.value ? (formControl.value | currency) : 'Unlimited'}}\n</div>\n<ng-container *ngIf=\"formControl.value && ((editMode$ | async)===false || (editMode$ | async)===null)\">\n    <div *ngIf=\"hasRemaining\" class=\"fu-remains\" [class.fu-no-remains]=\"remaining<=0\"><div class=\"fu-remains-value\">{{remaining | currency}}</div> remains</div>\n</ng-container>\n\n", styles: ["@font-face{font-family:NHaasGroteskDSPro;src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.eot);src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-75Bd.ttf) format(\"truetype\")}@font-face{font-family:NHaasGroteskDSPro-65Md;src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.eot);src:url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/NHaasGroteskDSPro-65Md.ttf) format(\"truetype\")}@font-face{font-family:HelveticaNeue;src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.eot);src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue.ttf) format(\"truetype\")}@font-face{font-family:HelveticaNeue-Medium;src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.eot);src:url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.eot?#iefix) format(\"embedded-opentype\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.woff2) format(\"woff2\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.woff) format(\"woff\"),url(https://fusion.ironsrc.net/assets/fonts/HelveticaNeue-Medium.ttf) format(\"truetype\")}.font-heading-6,.font-heading-5,.font-heading-4,.font-heading-3,.font-heading-2,.font-heading-1{font-family:NHaasGroteskDSPro,sans-serif}.font-heading-1{font-size:60px}.font-heading-2{font-size:32px}.font-heading-3{font-size:22px}.font-heading-4{font-size:18px}.font-heading-5{font-size:15px}.font-heading-6{font-size:13px}.font-heading-label{font-size:11px;font-family:NHaasGroteskDSPro-65Md,sans-serif;letter-spacing:.25px}.font-caption,.font-bodyXSmall,:host,.font-bodySmall,.font-bodyMedium,.font-bodyLarge{font-family:HelveticaNeue,helvetica,sans-serif;line-height:1.5}.font-bodyXSmallStrong,.font-bodySmallStrong,.font-bodyMediumStrong,.font-bodyLargeStrong{font-family:HelveticaNeue-Medium,helvetica,sans-serif;line-height:1.5}.font-bodyLarge,.font-bodyLargeStrong{font-size:16px}.font-bodyMedium,.font-bodyMediumStrong{font-size:14px}:host,.font-bodySmall{font-size:13px;letter-spacing:.24px}.font-bodySmallStrong{font-size:13px;letter-spacing:.24px}.font-bodyXSmall,.font-bodyXSmallStrong{font-size:12px;letter-spacing:.24px}.font-caption{font-size:11px}:host{display:flex;flex-direction:column;justify-content:center;max-width:150px;height:48px;margin:8px 0;min-width:140px;border-radius:8px}:host fusion-input-inline{display:none}:host .fu-value{padding:4px 8px 4px 4px;display:flex;align-items:center;justify-content:flex-end;height:24px;cursor:pointer;-webkit-user-select:none;user-select:none}:host .fu-remains{padding:0 8px;display:flex;flex-direction:row;justify-content:flex-end;gap:4px;color:var(--fu-light-900, #7B838C);-webkit-user-select:none;user-select:none;line-height:14px}:host .fu-remains.fu-no-remains{color:var(--fu-negative-700, #DA564F)}:host .fu-value:first-letter,:host .fu-remains-value:first-letter{padding-right:.3em;-webkit-user-select:none;user-select:none}:host.fu-show-edit fusion-input-inline{display:unset}:host.fu-show-edit .fu-value{display:none}:host-context(tr:hover){background-color:var(--fu-light-200, #EDEFF0)}:host-context(tr:hover).fu-show-edit{background-color:transparent}\n"] }]
    }], null, { inputInlineComponent: [{
            type: ViewChild,
            args: ['inputInline']
        }], data: [{
            type: Input
        }], cellPosition: [{
            type: Input
        }], remaining: [{
            type: Input
        }], dataChange: [{
            type: Output
        }], showInput: [{
            type: HostBinding,
            args: ['class.fu-show-edit']
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(CustomCellEditComponent, { className: "CustomCellEditComponent", filePath: "custom-cell-edit.component.ts", lineNumber: 16 }); })();

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

export { CustomCellEditComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-table-v3-stories-custom-cell-edit.mjs.map
