"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    clearOrderEntrySessionData: function() {
        return clearOrderEntrySessionData;
    },
    getEntrySessionStorageInfo: function() {
        return getEntrySessionStorageInfo;
    },
    setEntrySessionStorage: function() {
        return setEntrySessionStorage;
    }
});
const _hooks = require("@orderly.network/hooks");
const _types = require("@orderly.network/types");
function readValue(key, initialValue) {
    // Prevent build error "window is undefined" but keep keep working
    if (typeof window === "undefined") {
        return initialValue;
    }
    try {
        const item = window.sessionStorage.getItem(key);
        return item ? (0, _hooks.parseJSON)(item) : initialValue;
    } catch (error) {
        ;
        return initialValue;
    }
}
function getEntrySessionStorageInfo() {
    const orderType = readValue("order_type_key", _types.OrderType.LIMIT);
    const reduceOnly = readValue("reduce_only_key", false);
    const orderSide = readValue("side_key", _types.OrderSide.BUY);
    const orderTypeExt = readValue("order_type_ext_key", undefined);
    const sessionOrderEntry = readValue("order_entry_info", {
        "order_price": "",
        "order_quantity": "",
        "trigger_price": ""
    });
    return {
        orderType,
        reduceOnly,
        orderSide,
        orderTypeExt,
        sessionOrderEntry
    };
}
function setEntrySessionStorage(key, value) {
    const keys = [
        "order_type",
        "reduce_only",
        "side",
        "order_type_ext"
    ];
    if (keys.includes(key)) {
        sessionStorage.setItem(`${key}_key`, JSON.stringify(value));
    }
    const include = [
        "order_price",
        "order_quantity",
        "trigger_price"
    ].includes(key);
    if (include) {
        const sessionOrderEntry = readValue("order_entry_info", {
            "order_price": "",
            "order_quantity": "",
            "trigger_price": ""
        });
        sessionOrderEntry[key] = value;
        sessionStorage.setItem("order_entry_info", JSON.stringify(sessionOrderEntry));
    }
}
function clearOrderEntrySessionData() {
    setEntrySessionStorage("order_price", {
        "order_price": "",
        "order_quantity": "",
        "trigger_price": ""
    });
}

//# sourceMappingURL=entrySessionStorageUtils.js.map