import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

var StorageType;
(function (StorageType) {
    /**
     * No Cache
     */
    StorageType[StorageType["None"] = 0] = "None";
    /**
     * Data stored in Javascript object
     * Will be cleared when page is been refreshed
     */
    StorageType[StorageType["Application"] = 1] = "Application";
    /**
     * Data stored in browser session storage
     * https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
     */
    StorageType[StorageType["SessionStorage"] = 2] = "SessionStorage";
    /**
     * Data stored in browser local storage
     * Will be cleared when application says or user cleared manually.
     * https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
     */
    StorageType[StorageType["LocalStorage"] = 3] = "LocalStorage";
})(StorageType || (StorageType = {}));

class StorageService {
    constructor() {
        this.cacheObject = {};
    }
    get(cacheType, keyName) {
        let data = null;
        switch (cacheType) {
            case StorageType.Application:
                if (!isNullOrUndefined(this.cacheObject[keyName])) {
                    data = this.cacheObject[keyName];
                }
                break;
            case StorageType.LocalStorage:
                const rawLSData = window.localStorage.getItem(keyName);
                data = JSON.parse(rawLSData ? this._b64DecodeUnicode(rawLSData) : rawLSData);
                break;
            case StorageType.SessionStorage:
                const rawSSData = window.sessionStorage.getItem(keyName);
                data = JSON.parse(rawSSData ? this._b64DecodeUnicode(rawSSData) : rawSSData);
                break;
        }
        return data;
    }
    set(cacheType, keyName, data) {
        switch (cacheType) {
            case StorageType.Application:
                this.cacheObject[keyName] = data;
                break;
            case StorageType.LocalStorage:
                window.localStorage.setItem(keyName, this._b64EncodeUnicode(JSON.stringify(data)));
                break;
            case StorageType.SessionStorage:
                window.sessionStorage.setItem(keyName, this._b64EncodeUnicode(JSON.stringify(data)));
                break;
        }
    }
    remove(cacheType, keyName) {
        switch (cacheType) {
            case StorageType.Application:
                if (this.cacheObject[keyName]) {
                    delete this.cacheObject[keyName];
                }
                break;
            case StorageType.LocalStorage:
                window.localStorage.removeItem(keyName);
                break;
            case StorageType.SessionStorage:
                window.sessionStorage.removeItem(keyName);
                break;
        }
    }
    clearCache() {
        this.cacheObject = {};
        localStorage.clear();
        sessionStorage.clear();
    }
    _b64EncodeUnicode(str) {
        return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(Number('0x' + p1))));
    }
    _b64DecodeUnicode(str) {
        return decodeURIComponent(atob(str)
            .split('')
            .map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
            .join(''));
    }
    static { this.ɵfac = function StorageService_Factory(t) { return new (t || StorageService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: StorageService, factory: StorageService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(StorageService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], null, null); })();

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

export { StorageService, StorageType };
//# sourceMappingURL=ironsource-fusion-ui-services-stogare.mjs.map
