"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetShoppingCart = exports.RemoveShoppingCartFromLocalStorage = exports.SaveShoppingCart = exports.DeleteCartItem = exports.DecreaseCartItemAmount = exports.AddItemToShoppingCart = exports.IsCustomerLoggedIn = exports.GetObject = exports.DeleteAuthorizationToken = exports.IsJwtForRefresh = exports.SetAuthorizationTokenLocalStorage = exports.SetHttpAuthorizationToken = void 0;
var jwtDecode = require("jwt-decode");
var httpService_1 = require("./httpService");
var shoppingCartName = "ShoppingCart";
function SetHttpAuthorizationToken() {
    httpService_1.default.setAuthorizationToken(GetAuthorizationToken());
}
exports.SetHttpAuthorizationToken = SetHttpAuthorizationToken;
// save authorization token to local storage
function SetAuthorizationTokenLocalStorage(authorizationToken) {
    localStorage.setItem('authorizationToken', authorizationToken);
    SetHttpAuthorizationToken();
}
exports.SetAuthorizationTokenLocalStorage = SetAuthorizationTokenLocalStorage;
function IsJwtForRefresh() {
    var jwt = GetObject();
    var exp = new Date(Date.now() + (5 * 60 * 1000));
    if (jwt === null) {
        return false;
    }
    return jwt.exp < exp.getTime() / 1000;
}
exports.IsJwtForRefresh = IsJwtForRefresh;
// delete authorization token from local storage
function DeleteAuthorizationToken() {
    localStorage.removeItem('authorizationToken');
    SetHttpAuthorizationToken();
}
exports.DeleteAuthorizationToken = DeleteAuthorizationToken;
// get authorization token from local storage
function GetAuthorizationToken() {
    var authorizationToken = localStorage.getItem('authorizationToken');
    return authorizationToken !== null && !IsJwtExpired(authorizationToken) ? authorizationToken : null;
}
// Get user from jwtToken
function GetObject() {
    var token = GetAuthorizationToken();
    if (token === null) {
        return null;
    }
    var user = jwtDecode.default(token);
    var keyNames = Object.keys(user);
    var roleKey = keyNames.find(function (i) { return i.includes("claims/role"); });
    var uniqueNameKey = keyNames.find(function (i) { return i.includes("claims/name"); });
    if (roleKey !== undefined && uniqueNameKey !== undefined) {
        user = renameProperty(user, roleKey, 'role');
        user = renameProperty(user, uniqueNameKey, 'unique_name');
        return user;
    }
    return null;
}
exports.GetObject = GetObject;
function renameProperty(object, key, newKey) {
    var clonedObj = __assign({}, object);
    var targetKey = clonedObj[key];
    delete clonedObj[key];
    clonedObj[newKey] = targetKey;
    return clonedObj;
}
function IsCustomerLoggedIn() {
    var user = GetObject();
    if (user === null) {
        return false;
    }
    // server mi posila nazev prop role a name jako "schemas..."
    // deje se tak v SecurityService metoda CreateToken
    return user.role === "Uživatel";
}
exports.IsCustomerLoggedIn = IsCustomerLoggedIn;
function IsJwtExpired(token) {
    var jwt = jwtDecode.default(token);
    var result = jwt.exp < (Date.now() / 1000);
    if (result) {
        DeleteAuthorizationToken();
    }
    return result;
}
function AddItemToShoppingCart(newItem) {
    var cartItems = GetShoppingCart();
    var item = cartItems.find(function (i) { return i.productID === newItem.productID; });
    if (item !== undefined) {
        var itemIndex = cartItems.indexOf(item);
        cartItems[itemIndex].amount += newItem.amount;
    }
    else {
        cartItems.push(newItem);
    }
    SaveShoppingCart(cartItems);
}
exports.AddItemToShoppingCart = AddItemToShoppingCart;
function DecreaseCartItemAmount(item) {
    var cartItems = GetShoppingCart();
    var cartItem = cartItems.find(function (i) { return i.productID === item.productID; });
    if (cartItem !== undefined) {
        var index = cartItems.indexOf(cartItem);
        cartItems[index].amount -= item.amount;
        SaveShoppingCart(cartItems);
    }
}
exports.DecreaseCartItemAmount = DecreaseCartItemAmount;
function DeleteCartItem(item) {
    var cartItems = GetShoppingCart()
        .filter(function (i) { return i.productID !== item.productID; });
    SaveShoppingCart(cartItems);
}
exports.DeleteCartItem = DeleteCartItem;
function SaveShoppingCart(cartItems) {
    localStorage.setItem(shoppingCartName, JSON.stringify(cartItems));
}
exports.SaveShoppingCart = SaveShoppingCart;
function RemoveShoppingCartFromLocalStorage() {
    localStorage.removeItem(shoppingCartName);
}
exports.RemoveShoppingCartFromLocalStorage = RemoveShoppingCartFromLocalStorage;
function GetShoppingCart() {
    var cartItems = localStorage.getItem(shoppingCartName);
    return cartItems !== null ? JSON.parse(cartItems) : [];
}
exports.GetShoppingCart = GetShoppingCart;
//# sourceMappingURL=localStorageService.js.map