import { SpaStorageKeyEnum } from './spa-storage-key.enum';

export class SpaStorage {
    private static get channelId() {
        return `${this.getSetting(SpaStorageKeyEnum.brand)?.toUpperCase()}_${this.getSetting(SpaStorageKeyEnum.channelType)?.toUpperCase()}`;
    }

    public static clearSetting<KeyEnum>(key: KeyEnum | SpaStorageKeyEnum): void {
        localStorage.removeItem(key as string);
    }

    public static getSetting<KeyEnum>(key: KeyEnum | SpaStorageKeyEnum): string | null {
        return localStorage.getItem(key as string);
    }

    public static setSetting<KeyEnum>(key: KeyEnum | SpaStorageKeyEnum, settingValue: string): void {
        localStorage.setItem(key as string, settingValue);
    }

    public static getChannelSetting<KeyEnum>(key: KeyEnum | SpaStorageKeyEnum): string | undefined {
        let storageObj;
        const storageObjStr = localStorage.getItem(SpaStorageKeyEnum.channelSettings);

        if (storageObjStr) {
            storageObj = JSON.parse(storageObjStr);
            if (!storageObj[this.channelId]) {
                return undefined;
            }
            return storageObj[this.channelId][key];
        } else {
            return undefined;
        }
    }

    public static setChannelSetting<KeyEnum>(key: KeyEnum | SpaStorageKeyEnum, settingValue: any): void {
        const companySettings = this.getSetting(SpaStorageKeyEnum.channelSettings);
        let storageObj;

        if (companySettings) {
            storageObj = JSON.parse(companySettings);
        } else {
            storageObj = {};
        }

        if (!storageObj[this.channelId]) {
            storageObj[this.channelId] = {};
        }

        storageObj[this.channelId][key] = settingValue;
        this.setSetting(SpaStorageKeyEnum.channelSettings, JSON.stringify(storageObj));
    }
}


