function StorageHandler(name, objectKey) {

    if (!name || !objectKey) throw new Error("[StorageHandler]: Missing name and objectKey")

    const OBJECT_KEY = objectKey
    const localStorageName = name;

    const setItem = (key, object) => window.localStorage.setItem(key, JSON.stringify(object))
    const getItem = (key) => JSON.parse(window.localStorage.getItem(key));
    const getStorage = () => getItem(localStorageName);

    const setOrderToStorage = (id) => {
        const storage = getStorage();


        if (!storage[OBJECT_KEY][id]) {
            storage[OBJECT_KEY][id] = {} // Initialize the order as an object
            setItem(localStorageName, storage);
        }
    }

    const getOrderInStorage = (id) => {
        const storage = getStorage();

        if (storage[OBJECT_KEY][id]) {
            return storage[OBJECT_KEY][id]
        }
    }

    const putRecordToOrderObject = (id, record) => {
        try {
            const storage = getStorage();
            const recordKey = Object.keys(record)[0]; // Extract the key from the new record, e.g., "00018"

            if (storage[OBJECT_KEY][id][recordKey]) {
                storage[OBJECT_KEY][id][recordKey].quantity += record[recordKey].quantity;
                storage[OBJECT_KEY][id][recordKey].price += record[recordKey].price;
            } else {
                storage[OBJECT_KEY][id] = { ...storage[OBJECT_KEY][id], ...record };
            }

            setItem(localStorageName, storage);
        } catch (err) {
            throw new Error("Missing id in " + localStorageName + " or ln")
        }
    }

    const deleteOrderFromStorage = (id) => {
        const storage = getStorage();

        if (storage[OBJECT_KEY][id]) {
            delete storage[OBJECT_KEY][id];
            setItem(localStorageName, storage);
        }
    };

    const prepareOrderToSend = (id) => {
        try {
            const orderItems = getOrderInStorage(id);
            return Object.entries(orderItems).map((item) => ({
                order_id: id,
                line_number: item[0],
                prmry_prfl_cd: item[1].fscNr,
                rep_id: item[1].repId,
                quantity: item[1].quantity,
                price: item[1].price,
                market: item[1].market,
                timestamp: item[1].timestamp
            }))
        } catch (err) {
            throw new Error("id don't exist in " + localStorageName)
        }
    }
    const validateAndUpdateOrder = (id, currentOrderBasket) => {
        const storage = getStorage();
        const storedOrder = storage[OBJECT_KEY][id];

        if (!storedOrder) return;

        const basketLineNrs = new Set(currentOrderBasket.map(item => item.lineNr));

        // remove lineNr from storageOrder if don't exist in currentOrderBasket
        Object.keys(storedOrder).forEach(storedLineNr => {
            if (!basketLineNrs.has(storedLineNr)) {
                delete storedOrder[storedLineNr];
            }
        });

        currentOrderBasket.forEach(item => {
            const lineNr = item.lineNr;
            const qty = item.qty;
            const price = item.prc;

            if (storedOrder[lineNr]) {
                if (qty <= storedOrder[lineNr].quantity) {
                    storedOrder[lineNr].quantity = qty;  // Override quantity if lower or equal
                }
                if (price <= storedOrder[lineNr].price) {
                    storedOrder[lineNr].price = price;  // Override price if lower or equal
                }
            }
        });
        setItem(localStorageName, storage);

    }


    const init = () => {
        if (!getItem(localStorageName)) {
            const object = {
                [OBJECT_KEY]: {}
            };
            setItem(localStorageName, object);
        }
    }

    init();

    return {
        getStorage,
        setOrderToStorage,
        getOrderInStorage,
        putRecordToOrderObject,
        deleteOrderFromStorage,
        prepareOrderToSend,
        validateAndUpdateOrder
    }
}