import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Directive, Inject, Input, Output, ViewChild } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import * as i1 from '@ironsource/fusion-ui/services/unique-id';
import * as i3 from '@ironsource/fusion-ui/services/log';
import * as i2 from '@ironsource/fusion-ui/services/window';
import { isFunction } from '@ironsource/fusion-ui/utils';

function getDefaultCssUnit(value) {
    return /^\d+$/.test(value) ? `${value}px` : value;
}

class ModalService {
    constructor() {
        this.modals = {};
    }
    add(modal) {
        // add modal to array of active modals
        this.modals[modal.id] = modal;
    }
    remove(id) {
        // remove modal from array of active modals
        delete this.modals[id];
    }
    open(id) {
        if (this.modals[id] && isFunction(this.modals[id].open)) {
            this.modals[id].open();
        }
    }
    close(id, { emitEvent = true } = {}) {
        if (this.modals[id] && isFunction(this.modals[id].close)) {
            this.modals[id].close(emitEvent);
        }
    }
    static { this.ɵfac = function ModalService_Factory(t) { return new (t || ModalService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ModalService, factory: ModalService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ModalService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], null, null); })();

const _c0 = ["modalBody"];
const _c1 = ["modalHolder"];
class ModalBaseComponent {
    set width(value) {
        if (value) {
            this._width = getDefaultCssUnit(value);
        }
    }
    get width() {
        return this._width;
    }
    set height(value) {
        if (value) {
            this._height = getDefaultCssUnit(value);
        }
    }
    get height() {
        return this._height;
    }
    constructor(document, uidService, elRef, windowRef, logService, modalService, renderer) {
        this.document = document;
        this.uidService = uidService;
        this.elRef = elRef;
        this.windowRef = windowRef;
        this.logService = logService;
        this.modalService = modalService;
        this.renderer = renderer;
        this.footer = true;
        this.loading = false; // state for content loading
        this.waiting = false; // state for on click primary button waiter
        this.error = '';
        this.headerText = '';
        this.noHeaderBorder = false;
        this.isClosed = false;
        this.saveButtonText = 'Save';
        this.saveButtonDisabled = false;
        this.cancelButtonText = 'Cancel';
        // eslint-disable-next-line
        this.onSave = new EventEmitter();
        // eslint-disable-next-line
        this.onOpen = new EventEmitter();
        // eslint-disable-next-line
        this.onClose = new EventEmitter();
        this.uid = this.uidService.getUniqueId().toString();
    }
    ngOnInit() {
        if (!this.id) {
            this.logService.error(new Error('Modal component must have an id'));
            return;
        }
        if (this.isClosed) {
            this.close(false);
        }
        this.modalService.add(this);
    }
    ngOnDestroy() {
        this.modalService.remove(this.id);
    }
    open() {
        this.renderer.setStyle(this.elRef.nativeElement, 'display', 'block');
        this.onOpen.emit();
    }
    close(emitEvent = true) {
        this.renderer.setStyle(this.elRef.nativeElement, 'display', 'none');
        if (emitEvent) {
            this.onClose.emit();
        }
    }
    save(value) {
        this.onSave.emit(value);
    }
    static { this.ɵfac = function ModalBaseComponent_Factory(t) { return new (t || ModalBaseComponent)(i0.ɵɵdirectiveInject(DOCUMENT), i0.ɵɵdirectiveInject(i1.UniqueIdService), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i2.WindowService), i0.ɵɵdirectiveInject(i3.LogService), i0.ɵɵdirectiveInject(ModalService), i0.ɵɵdirectiveInject(i0.Renderer2)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: ModalBaseComponent, viewQuery: function ModalBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 7);
            i0.ɵɵviewQuery(_c1, 7);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.modalBody = _t.first);
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.modalHolder = _t.first);
        } }, inputs: { id: "id", footer: "footer", loading: "loading", waiting: "waiting", width: "width", height: "height", padding: "padding", error: "error", headerText: "headerText", noHeaderBorder: "noHeaderBorder", isClosed: "isClosed", saveButtonText: "saveButtonText", saveButtonDisabled: "saveButtonDisabled", cancelButtonText: "cancelButtonText", cancelButtonHidden: "cancelButtonHidden" }, outputs: { onSave: "onSave", onOpen: "onOpen", onClose: "onClose" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ModalBaseComponent, [{
        type: Directive
    }], () => [{ type: Document, decorators: [{
                type: Inject,
                args: [DOCUMENT]
            }] }, { type: i1.UniqueIdService }, { type: i0.ElementRef }, { type: i2.WindowService }, { type: i3.LogService }, { type: ModalService }, { type: i0.Renderer2 }], { id: [{
            type: Input
        }], footer: [{
            type: Input
        }], loading: [{
            type: Input
        }], waiting: [{
            type: Input
        }], width: [{
            type: Input
        }], height: [{
            type: Input
        }], padding: [{
            type: Input
        }], error: [{
            type: Input
        }], headerText: [{
            type: Input
        }], noHeaderBorder: [{
            type: Input
        }], isClosed: [{
            type: Input
        }], saveButtonText: [{
            type: Input
        }], saveButtonDisabled: [{
            type: Input
        }], cancelButtonText: [{
            type: Input
        }], cancelButtonHidden: [{
            type: Input
        }], onSave: [{
            type: Output
        }], onOpen: [{
            type: Output
        }], onClose: [{
            type: Output
        }], modalBody: [{
            type: ViewChild,
            args: ['modalBody', { static: true }]
        }], modalHolder: [{
            type: ViewChild,
            args: ['modalHolder', { static: true }]
        }] }); })();

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

export { ModalBaseComponent, ModalService };
//# sourceMappingURL=ironsource-fusion-ui-components-modal-common-base.mjs.map
