"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateGroupProfile = void 0;
const tslib_1 = require("tslib");
const axios_1 = require("axios");
const helpers_1 = require("../helpers");
const constants_1 = require("../constants");
const helpers_2 = require("./helpers");
const CryptoJS = require("crypto-js");
const getGroupInfo_1 = require("./getGroupInfo");
/**
 * Update Group Profile
 */
const updateGroupProfile = (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const { chatId, groupName, groupImage, groupDescription, rules, account = null, signer = null, env = constants_1.default.ENV.PROD, pgpPrivateKey = null, } = options || {};
    try {
        /**
         * VALIDATIONS
         */
        if (account == null && signer == null) {
            throw new Error(`At least one from account or signer is necessary!`);
        }
        const wallet = (0, helpers_2.getWallet)({ account, signer });
        const address = yield (0, helpers_2.getAccountAddress)(wallet);
        (0, helpers_2.updateGroupRequestValidator)(chatId, groupName, [], [], address, groupDescription);
        const group = yield (0, getGroupInfo_1.getGroupInfo)({
            chatId,
            env,
        });
        /**
         * CREATE PROFILE VERIFICATION PROOF
         */
        const bodyToBeHashed = {
            groupName: groupName,
            groupDescription: groupDescription,
            groupImage: groupImage,
            rules: rules !== null && rules !== void 0 ? rules : {},
            isPublic: group.isPublic,
            groupType: group.groupType,
        };
        const hash = CryptoJS.SHA256(JSON.stringify(bodyToBeHashed)).toString();
        const connectedUser = yield (0, helpers_2.getConnectedUserV2)(wallet, pgpPrivateKey, env);
        const signature = yield (0, helpers_2.sign)({
            message: hash,
            signingKey: connectedUser.privateKey,
        });
        const sigType = 'pgpv2';
        // Account is need to verify the signature at any future point
        const profileVerificationProof = sigType + ':' + signature + ':' + connectedUser.did;
        /**
         * API CALL TO PUSH NODES
         */
        const API_BASE_URL = (0, helpers_1.getAPIBaseUrls)(env);
        const apiEndpoint = `${API_BASE_URL}/v1/chat/groups/${chatId}/profile`;
        const { isPublic: is_public, groupType: group_type } = bodyToBeHashed, body = tslib_1.__rest(bodyToBeHashed, ["isPublic", "groupType"]);
        body.profileVerificationProof = profileVerificationProof;
        return axios_1.default
            .put(apiEndpoint, body)
            .then((response) => {
            return response.data;
        })
            .catch((err) => {
            var _a, _b;
            if ((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data)
                throw new Error((_b = err === null || err === void 0 ? void 0 : err.response) === null || _b === void 0 ? void 0 : _b.data);
            throw new Error(err);
        });
    }
    catch (err) {
        console.error(`[Push SDK] - API  - Error - API ${exports.updateGroupProfile.name} -:  `, err);
        throw Error(`[Push SDK] - API  - Error - API ${exports.updateGroupProfile.name} -: ${err}`);
    }
});
exports.updateGroupProfile = updateGroupProfile;
//# sourceMappingURL=updateGroupProfile.js.map