"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGroupCore = exports.createGroup = 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 createGroup = (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    return yield (0, exports.createGroupCore)(options, helpers_2.PGPHelper);
});
exports.createGroup = createGroup;
const createGroupCore = (options, pgpHelper) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const { account = null, signer = null, groupName, groupDescription, members, groupImage, admins, isPublic, contractAddressNFT, numberOfNFTs, contractAddressERC20, numberOfERC20, env = constants_1.default.ENV.PROD, pgpPrivateKey = null, meta, groupType, scheduleAt, scheduleEnd, rules, } = options || {};
    try {
        if (account == null && signer == null) {
            throw new Error(`At least one from account or signer is necessary!`);
        }
        (0, helpers_2.validateScheduleDates)(scheduleAt, scheduleEnd);
        const wallet = (0, helpers_2.getWallet)({ account, signer });
        (0, helpers_2.createGroupRequestValidator)(groupName, members, admins, groupDescription, contractAddressNFT, numberOfNFTs, contractAddressERC20, numberOfERC20);
        const convertedMembersPromise = members.map((each) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            return (0, helpers_2.getUserDID)(each, env);
        }));
        const convertedAdminsPromise = admins.map((each) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            return (0, helpers_2.getUserDID)(each, env);
        }));
        const convertedMembers = yield Promise.all(convertedMembersPromise);
        const convertedAdmins = yield Promise.all(convertedAdminsPromise);
        const connectedUser = yield (0, helpers_2.getConnectedUserV2Core)(wallet, pgpPrivateKey, env, pgpHelper);
        const bodyToBeHashed = {
            groupName: groupName,
            groupDescription: groupDescription == undefined ? null : groupDescription,
            members: convertedMembers,
            groupImage: groupImage == undefined ? null : groupImage,
            admins: convertedAdmins,
            isPublic: isPublic,
            contractAddressNFT: contractAddressNFT == undefined ? null : contractAddressNFT,
            numberOfNFTs: numberOfNFTs == undefined ? 0 : numberOfNFTs,
            contractAddressERC20: contractAddressERC20 == undefined ? null : contractAddressERC20,
            numberOfERC20: numberOfERC20 == undefined ? 0 : numberOfERC20,
            groupCreator: connectedUser.did,
        };
        const hash = CryptoJS.SHA256(JSON.stringify(bodyToBeHashed)).toString();
        const signature = yield pgpHelper.sign({
            message: hash,
            signingKey: connectedUser.privateKey,
        });
        const sigType = 'pgp';
        const verificationProof = sigType + ':' + signature;
        const API_BASE_URL = (0, helpers_1.getAPIBaseUrls)(env);
        const apiEndpoint = `${API_BASE_URL}/v1/chat/groups`;
        const body = (0, helpers_2.createGroupPayload)(groupName, convertedMembers, convertedAdmins, isPublic, connectedUser.did, verificationProof, groupDescription, groupImage, contractAddressNFT, numberOfNFTs, contractAddressERC20, numberOfERC20, meta, groupType, scheduleAt, scheduleEnd, rules);
        return axios_1.default
            .post(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.createGroup.name} -:  `, err);
        throw Error(`[Push SDK] - API  - Error - API ${exports.createGroup.name} -: ${err}`);
    }
});
exports.createGroupCore = createGroupCore;
//# sourceMappingURL=createGroup.js.map