"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Delegate = void 0;
const tslib_1 = require("tslib");
const config_1 = require("../config"), config = config_1;
const PUSH_CHANNEL = require("../channels");
const helpers_1 = require("../helpers");
const pushNotificationBase_1 = require("./pushNotificationBase");
class Delegate extends pushNotificationBase_1.PushNotificationBaseClass {
    constructor(signer, env, account) {
        super(signer, env, account);
        /**
         * @description - Get delegates of a channell
         * @param {string} [options.channel] - channel in caip. defaults to account from signer with eth caip
         * @returns array of delegates
         */
        this.get = (options) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                // const {
                //   channel = this.account
                //     ? getFallbackETHCAIPAddress(this.env!, this.account!)
                //     : null,
                // } = options || {};
                let channel = (options === null || options === void 0 ? void 0 : options.channel)
                    ? options.channel
                    : this.account
                        ? (0, helpers_1.getFallbackETHCAIPAddress)(this.env, this.account)
                        : null;
                this.checkUserAddressExists(channel);
                channel = (0, helpers_1.validateCAIP)(channel)
                    ? channel
                    : (0, helpers_1.getFallbackETHCAIPAddress)(this.env, channel);
                this.checkUserAddressExists(channel);
                return yield PUSH_CHANNEL.getDelegates({
                    channel: channel,
                    env: this.env,
                });
            }
            catch (error) {
                throw new Error(`Push SDK Error: API : delegate::get : ${error}`);
            }
        });
        /**
         * @description adds a delegate
         * @param {string} delegate - delegate address in caip to be added
         * @returns the transaction hash if the transaction is successfull
         */
        this.add = (delegate) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                this.checkSignerObjectExists();
                if (this.signer && !this.signer.provider) {
                    throw new Error('Provider is required');
                }
                if ((0, helpers_1.validateCAIP)(delegate)) {
                    delegate = this.getAddressFromCaip(delegate);
                }
                const networkDetails = yield this.getChainId(this.signer);
                const caip = `eip155:${networkDetails}`;
                if (!config_1.default[this.env][caip] || !config.VIEM_CONFIG[this.env][caip]) {
                    throw new Error('Unsupported Chainid');
                }
                const commAddress = config_1.default[this.env][caip].EPNS_COMMUNICATOR_CONTRACT;
                const commContract = this.createContractInstance(commAddress, config.ABIS.COMM, config.VIEM_CONFIG[this.env][caip].NETWORK);
                const addDelegateRes = yield this.addDelegator(commContract, delegate);
                return { transactionHash: addDelegateRes };
            }
            catch (error) {
                throw new Error(`Push SDK Error: Contract : delegate::add : ${error}`);
            }
        });
        /**
         * @description removes a delegate
         * @param {string} delegate - caip address of the delegate to be removed
         * @returns the transaction hash if the transaction is successfull
         */
        this.remove = (delegate) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                this.checkSignerObjectExists();
                if (this.signer && !this.signer.provider) {
                    throw new Error('Provider is required');
                }
                if ((0, helpers_1.validateCAIP)(delegate)) {
                    delegate = this.getAddressFromCaip(delegate);
                }
                const networkDetails = yield this.getChainId(this.signer);
                const caip = `eip155:${networkDetails}`;
                if (!config_1.default[this.env][caip] || !config.VIEM_CONFIG[this.env][caip]) {
                    throw new Error('Unsupported Chainid');
                }
                const commAddress = config_1.default[this.env][caip].EPNS_COMMUNICATOR_CONTRACT;
                const commContract = this.createContractInstance(commAddress, config.ABIS.COMM, config.VIEM_CONFIG[this.env][caip].NETWORK);
                const removeDelegateRes = yield this.removeDelegator(commContract, delegate);
                return { transactionHash: removeDelegateRes };
            }
            catch (error) {
                throw new Error(`Push SDK Error: Contract : delegate::remove : ${error}`);
            }
        });
    }
}
exports.Delegate = Delegate;
//# sourceMappingURL=delegate.js.map