import * as i0 from '@angular/core';
import { InjectionToken, Component, ChangeDetectionStrategy, Optional, Inject, Input, NgModule } from '@angular/core';
import * as i2 from '@ironsource/fusion-ui/services/log';
import * as i1 from '@angular/common/http';
import { withFetch, provideHttpClient, HttpClientModule } from '@angular/common/http';
import { Subject } from 'rxjs';
import { takeUntil, publishReplay, refCount, finalize } from 'rxjs/operators';
import * as i3 from '@ironsource/fusion-ui/services/stogare';
import { StorageType } from '@ironsource/fusion-ui/services/stogare';
import { CommonModule } from '@angular/common';

const SVG_OPTIONS_DEFAULT_VALUES = {
    assetsPath: ''
};
const SVG_OPTIONS_TOKEN = new InjectionToken('SVG options');

const _c0 = ["*"];
class SvgComponent {
    // inputs
    set path(value) {
        this.onPathChanged(value);
    }
    static { this.cache = {}; }
    constructor(httpClient, elementRef, renderer, logService, storageService, svgOptions) {
        this.httpClient = httpClient;
        this.elementRef = elementRef;
        this.renderer = renderer;
        this.logService = logService;
        this.storageService = storageService;
        this.svgOptions = svgOptions;
        this.libVersion = 'v3';
        this.svgPath = '';
        this.onDestroy$ = new Subject();
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
    }
    getUrlPath() {
        let assetPath = '';
        if (this.svgOptions && this.svgOptions.assetsPath) {
            assetPath =
                this.svgOptions.assetsPath === 'https://fusion.ironsrc.net/assets'
                    ? `${this.svgOptions.assetsPath}/icons/`
                    : `${this.svgOptions.assetsPath}`;
        }
        if (this.svgPath === '') {
            throw new Error(`Asset path-name '${this.svgPath}' not set`);
        }
        else if (!this.svgPath.endsWith('.svg')) {
            // check for .svg if no, add
            this.svgPath += '.svg';
        }
        if (this.svgPath.startsWith('assets/')) {
            assetPath = assetPath.replace('assets/icons/', '');
        }
        return `${assetPath}${this.svgPath}`;
    }
    ngAfterViewInit() {
        this.loadSvg();
    }
    loadSvg() {
        const svgUrl = this.getUrlPath();
        if (svgUrl) {
            const cachedData = this.storageService.get(StorageType.SessionStorage, `${svgUrl}`);
            if (!!cachedData) {
                this.elementRef.nativeElement.innerHTML = cachedData;
            }
            else {
                this.getSvgData(svgUrl).subscribe(response => {
                    this.storageService.set(StorageType.SessionStorage, `${svgUrl}`, response);
                    this.elementRef.nativeElement.innerHTML = response;
                }, err => {
                    this.logService.error(new Error(`Error Fetching Svg: ${svgUrl}, ${JSON.stringify(err)}`));
                });
            }
        }
    }
    getSvgData(svgUrl) {
        if (!SvgComponent.cache[svgUrl]) {
            SvgComponent.cache[svgUrl] = this.httpClient.get(svgUrl, { responseType: 'text' }).pipe(takeUntil(this.onDestroy$), publishReplay(1), refCount(), finalize(() => {
                delete SvgComponent.cache[svgUrl];
            }));
        }
        return SvgComponent.cache[svgUrl];
    }
    onPathChanged(value) {
        this.svgPath = value;
        if (this.elementRef?.nativeElement) {
            this.loadSvg();
        }
    }
    static { this.ɵfac = function SvgComponent_Factory(t) { return new (t || SvgComponent)(i0.ɵɵdirectiveInject(i1.HttpClient), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.Renderer2), i0.ɵɵdirectiveInject(i2.LogService), i0.ɵɵdirectiveInject(i3.StorageService), i0.ɵɵdirectiveInject(SVG_OPTIONS_TOKEN, 8)); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: SvgComponent, selectors: [["fusion-svg"]], inputs: { path: "path" }, ngContentSelectors: _c0, decls: 1, vars: 0, template: function SvgComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵprojectionDef();
            i0.ɵɵprojection(0);
        } }, encapsulation: 2, changeDetection: 0 }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SvgComponent, [{
        type: Component,
        args: [{ selector: 'fusion-svg', changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-content></ng-content>\n" }]
    }], () => [{ type: i1.HttpClient }, { type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i2.LogService }, { type: i3.StorageService }, { type: undefined, decorators: [{
                type: Optional
            }, {
                type: Inject,
                args: [SVG_OPTIONS_TOKEN]
            }] }], { path: [{
            type: Input
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(SvgComponent, { className: "SvgComponent", filePath: "svg.component.ts", lineNumber: 15 }); })();

class SvgModule {
    static forRoot(options) {
        return {
            ngModule: SvgModule,
            providers: [
                {
                    provide: SVG_OPTIONS_TOKEN,
                    useValue: options ? options : SVG_OPTIONS_DEFAULT_VALUES,
                    ...options
                }
            ]
        };
    }
    static { this.ɵfac = function SvgModule_Factory(t) { return new (t || SvgModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: SvgModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ providers: [provideHttpClient(withFetch())], imports: [CommonModule, HttpClientModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SvgModule, [{
        type: NgModule,
        args: [{
                declarations: [SvgComponent],
                exports: [SvgComponent],
                imports: [CommonModule, HttpClientModule],
                providers: [provideHttpClient(withFetch())]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(SvgModule, { declarations: [SvgComponent], imports: [CommonModule, HttpClientModule], exports: [SvgComponent] }); })();

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

export { SVG_OPTIONS_DEFAULT_VALUES, SVG_OPTIONS_TOKEN, SvgComponent, SvgModule };
//# sourceMappingURL=ironsource-fusion-ui-components-svg.mjs.map
