"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var utils_1 = require("@joincivil/utils");
var storageKeys = {};
window.addEventListener(utils_1.CIVIL_LOCAL_STORAGE_EVENTS.SET_ITEM, function (ev) {
    var _a = ev.detail, updatedKey = _a.key, updatedValue = _a.value;
    var handler = storageKeys[updatedKey + "::" + utils_1.CIVIL_LOCAL_STORAGE_EVENTS.SET_ITEM];
    if (handler) {
        handler(updatedValue);
    }
    ev.stopPropagation();
}, false);
window.addEventListener(utils_1.CIVIL_LOCAL_STORAGE_EVENTS.UPDATE_ITEM, function (ev) {
    var _a = ev.detail, updatedKey = _a.key, updatedValue = _a.value;
    var handler = storageKeys[updatedKey + "::" + utils_1.CIVIL_LOCAL_STORAGE_EVENTS.UPDATE_ITEM];
    if (typeof handler === "function") {
        handler(updatedValue);
    }
    ev.stopPropagation();
}, false);
utils_1.startLocalStorageUpdateBroadcast();
function areValuesEqual(prevValue, newValue) {
    if (typeof prevValue === "object" && typeof newValue === "object") {
        var isEqual = prevValue === newValue;
        if (!isEqual) {
            var prevValueKeys = Object.keys(prevValue);
            var newValueKeys = Object.keys(newValue);
            if (prevValueKeys.length !== newValueKeys.length) {
                isEqual = false;
            }
            else {
                isEqual = true;
                for (var _i = 0, prevValueKeys_1 = prevValueKeys; _i < prevValueKeys_1.length; _i++) {
                    var k = prevValueKeys_1[_i];
                    if (prevValue[k] !== newValue[k]) {
                        isEqual = false;
                        break;
                    }
                }
            }
            return isEqual;
        }
    }
    return prevValue === newValue;
}
function useStateWithLocalStorage(key, defaultValue) {
    var _a = React.useState(function () {
        try {
            var item = utils_1.fetchItem(key);
            return item;
        }
        catch (err) {
            return defaultValue;
        }
    }), value = _a[0], setStateValue = _a[1];
    var setValue = function (newValue, skipLocalStorageUpdate) {
        setStateValue(newValue);
        if (!skipLocalStorageUpdate) {
            if (typeof value === "undefined" || value === null) {
                utils_1.removeItem(key);
            }
            else {
                utils_1.setItem(key, newValue);
            }
        }
    };
    var refreshValueFromLocalStorage = function () {
        try {
            var item = utils_1.fetchItem(key);
            setStateValue(item);
        }
        catch (err) {
            throw new Error("Error refreshing state value from localStorage, key=" + key);
        }
    };
    storageKeys[key + "::" + utils_1.CIVIL_LOCAL_STORAGE_EVENTS.SET_ITEM] = function (updatedValue) {
        if (!areValuesEqual(value, updatedValue)) {
            setStateValue(updatedValue);
        }
    };
    storageKeys[key + "::" + utils_1.CIVIL_LOCAL_STORAGE_EVENTS.UPDATE_ITEM] = function (updatedValue) {
        refreshValueFromLocalStorage();
    };
    return [value, setValue];
}
exports.default = useStateWithLocalStorage;
//# sourceMappingURL=useStateWithLocalStorage.js.map