import * as i0 from '@angular/core';
import { PLATFORM_ID, Injectable, Inject } from '@angular/core';
import { LocalStorageKeysEnum } from '@testlocallibrary/shared-kernel/models/enums';
import { isPlatformBrowser } from '@angular/common';
import * as i1 from '@testlocallibrary/shared-kernel/services/cart-service';
import * as CryptoJS from 'crypto-js';
import { LocalStorageKeys } from '@testlocallibrary/shared-kernel/models/view-models';

class LocalStorageService {
    constructor(platformId) {
        this.platformId = platformId;
        this.isBrowser = isPlatformBrowser(platformId);
    }
    saveToLocalStorage(key, value) {
        if (this.isBrowser) {
            localStorage.setItem(key, value);
        }
    }
    getFromLocalStorage(key) {
        if (this.isBrowser) {
            const item = localStorage.getItem(key);
            return item;
        }
        return null;
    }
    removeFromLocalStorage(key) {
        if (this.isBrowser) {
            localStorage.removeItem(key);
        }
    }
    clearLocalStorage() {
        if (this.isBrowser) {
            localStorage.clear();
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageService, deps: [{ token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }], ctorParameters: function () { return [{ type: Object, decorators: [{
                    type: Inject,
                    args: [PLATFORM_ID]
                }] }]; } });

class CartLocalStorageService {
    constructor(cartService, ngZone, localStorageService, platformId) {
        this.cartService = cartService;
        this.ngZone = ngZone;
        this.localStorageService = localStorageService;
        this.platformId = platformId;
        this.isBrowser = isPlatformBrowser(platformId);
    }
    initializeCartFromLocalStorage() {
        const saved = this.localStorageService.getFromLocalStorage(LocalStorageKeysEnum.Cart);
        if (saved != null) {
            const savedState = JSON.parse(saved) || {};
            if (savedState && savedState.cart) {
                this.cartService.addManyToCart(savedState.cart);
            }
        }
    }
    setupBeforeUnloadListener() {
        if (this.isBrowser) {
            window.addEventListener('storage', (event) => {
                if (event.key === LocalStorageKeysEnum.Cart) {
                    this.ngZone.run(() => {
                        this.syncLocalStorageWithStore();
                    });
                }
            });
        }
    }
    syncLocalStorageWithStore() {
        const cartItems = JSON.parse(this.localStorageService.getFromLocalStorage(LocalStorageKeysEnum.Cart) ||
            '{}').cart;
        this.cartService.sync(cartItems);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartLocalStorageService, deps: [{ token: i1.CartService }, { token: i0.NgZone }, { token: LocalStorageService }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartLocalStorageService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: CartLocalStorageService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }], ctorParameters: function () { return [{ type: i1.CartService }, { type: i0.NgZone }, { type: LocalStorageService }, { type: Object, decorators: [{
                    type: Inject,
                    args: [PLATFORM_ID]
                }] }]; } });

class LocalStorageEncryptionService {
    constructor(localStorageService) {
        this.localStorageService = localStorageService;
        this.secretKey = 'linkDataCenterWebsiteSecretKey';
    }
    addLocalStorageItem(itemKey, itemValue) {
        itemValue = this.encryptData(itemValue);
        this.localStorageService.saveToLocalStorage(itemKey, itemValue);
    }
    getLocalStorageItem(key) {
        let localStorageItem = this.localStorageService.getFromLocalStorage(key);
        if (localStorageItem == null)
            return '';
        let localStorageItemValue = this.decryptData(localStorageItem);
        return localStorageItemValue;
    }
    changeSelectedPlanId(planKey, planId) {
        let PlansIdsFromLocalStorage = this.getLocalStorageItem(LocalStorageKeysEnum.KeysName);
        let selectedPlans = new LocalStorageKeys();
        PlansIdsFromLocalStorage !== ''
            ? (selectedPlans = JSON.parse(PlansIdsFromLocalStorage))
            : null;
        selectedPlans[planKey] = planId;
        this.addLocalStorageItem(LocalStorageKeysEnum.KeysName, JSON.stringify(selectedPlans));
    }
    getSelectedPlanId(planKey) {
        let PlansIdsFromLocalStorage = this.getLocalStorageItem(LocalStorageKeysEnum.KeysName);
        if (PlansIdsFromLocalStorage == '')
            return 0;
        let selectedPlans = JSON.parse(PlansIdsFromLocalStorage);
        return selectedPlans[planKey];
    }
    encryptData(data) {
        let textToEncrypt = JSON.stringify(data);
        let encryptedData = CryptoJS.AES.encrypt(textToEncrypt, this.secretKey).toString();
        return encryptedData;
    }
    decryptData(encryptedData) {
        let decryptedBytes = CryptoJS.AES.decrypt(encryptedData, this.secretKey);
        let decryptedText = decryptedBytes.toString(CryptoJS.enc.Utf8);
        let decryptedData = JSON.parse(decryptedText);
        return decryptedData;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageEncryptionService, deps: [{ token: LocalStorageService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageEncryptionService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: LocalStorageEncryptionService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }], ctorParameters: function () { return [{ type: LocalStorageService }]; } });

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

export { CartLocalStorageService, LocalStorageEncryptionService, LocalStorageService };
//# sourceMappingURL=testlocallibrary-shared-kernel-services-local-storage.mjs.map
