import moment from 'moment';

let content = {};

const Store = {};
let MemoryStore = GetItem('memoryStore') || {};


/**
 * Sets item in localStorage under 'content' keyword
 * @param  {string} key
 * @param  {any} payload 
 */
export function SetItem(key, payload) {
    // let convertedPayload = typeof payload == 'string' ? payload : JSON.stringify(payload);
    content[key] = payload;
    localStorage.content = JSON.stringify(content);
}

/**
 * Returns data for particular key
 * @param  {string} key 
 * @param  {boolean} nonVolatile - (optional)
 */
export function GetItem(key, nonVolatile = false) {
    if (!key) {
        return null;
    }

    if (nonVolatile) {
        return nonVolatile[key];
    }
    return content[key];

}

export function GetTime({ dateTime, format } = {}) {
    let dt = moment();
    if (dateTime) {
        if (typeof dateTime == 'string') {
            dt = new Date(dateTime.replace(' ', 'T'));
        } else {
            dt = new Date(dateTime);
        }
    }

    return moment(dt).format(format || 'YYYY-MM-DD HH:mm:ss');
}

/*
 * Converts a string to a bool.
 * This conversion will:
 *
 *  - match 'true', 'on', or '1' as true.
 *  - ignore all white-space padding
 *  - ignore capitalization (case).
 *
 * '  tRue  ','ON', and '1   ' will all evaluate as true.
 */
export function StrToBool(s) {
    // will match one and only one of the string 'true','1', or 'on' rerardless
    // of capitalization and regardless off surrounding white-space.
    //
    const regex = /^\s*(true|1|on)\s*$/i;

    return regex.test(s);
}

/**
 * Event Broadcaster method
 * Event can be broadcasted without storing into state or temporarily
 * 
 * Expects eventName and data to be broadcasted
 * 
 * @param  {string} {eventName - unique identifier of event
 * @param  {any} data - to be broadcasted
 * @param  {any} objParams(optional) - extra tag to check if event is already subscribed 
 * @param  {boolean} isMemoryStore(optional)=false - if set to true, events will be persisted even after current session(for this to work, initialisation of localstorage util is required)
 * @param  {boolean} isTemp(optional)=false - determines if event is to be broadcasted only one time
 * @param  {boolean} dontTransmit(optional)=false - event will be stored with the data but wont be broadcasted immediately after update
 */
export function StoreEvent({ eventName, data, objParams, isMemoryStore = false, isTemp = false, dontTransmit = false }) {
    // new Promise((resolve, reject) => {
    const eventDetail = { eventName, data, objParams };
    if (!isTemp) { // temporary broadcastings needn't to be stored
        if (!isMemoryStore) {
            Store[eventName] = eventDetail;
        } else {
            MemoryStore = GetItem('memoryStore') || {};
            MemoryStore[eventName] = eventDetail;
            SetItem('memoryStore', MemoryStore);
        }
    }

    if (!dontTransmit) {
        // TransmitToAllEvent({ eventDetail, isMemoryStore });
    }
    // });
}