/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

import Vue from "vue";
import {SecurityKeyPair} from "~/modules/security/index";

export const state = Vue.observable({
    contactSelfKeys: {},
    contactRelatedKeys: {},
    chatRoomKeys: {}
});

export const mutations = {
    setContactSelfKeyPair(contactId: string, keyPair: SecurityKeyPair) {
        Vue.set(state.contactSelfKeys, contactId, keyPair);
    },

    setContactRelatedKey(contactId: string, key: string) {
        Vue.set(state.contactRelatedKeys, contactId, key);
    },

    setChatRoomKey(roomId: string, key: string) {
        Vue.set(state.chatRoomKeys, roomId, key);
    }
}

export const getters = {
    isContactEncrypted: (contactId: string) : boolean => {
        return state.contactSelfKeys.hasOwnProperty(contactId) &&
            state.contactRelatedKeys.hasOwnProperty(contactId);
    },
    contactSelfKeyPair: (contactId: string) : SecurityKeyPair | undefined => {
        return state.contactSelfKeys[contactId];
    },
    contactRelatedKey: (contactId: string) : string | undefined => {
        return state.contactRelatedKeys[contactId];
    },


    isRoomEncrypted: (roomId: string) => {
        return state.chatRoomKeys.hasOwnProperty(roomId);
    },
    roomKey: (roomId: string) => {
        return state.chatRoomKeys[roomId];
    }
}

