"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifyVenue = exports.getAuthorization = exports.prepareModifyVenue = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareModifyVenue(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx }, }, context, } = this;
        const { venue, description, type } = args;
        const { id: venueId } = venue;
        const { description: currentDescription, type: currentType } = yield venue.details();
        if (currentDescription === description) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'New description is the same as the current one',
            });
        }
        if (currentType === type) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.NoDataChange,
                message: 'New type is the same as the current one',
            });
        }
        if (description) {
            this.addTransaction(tx.settlement.updateVenueDetails, {}, conversion_1.numberToU64(venueId, context), conversion_1.stringToVenueDetails(description, context));
        }
        if (type) {
            this.addTransaction(tx.settlement.updateVenueType, {}, conversion_1.numberToU64(venueId, context), conversion_1.venueTypeToMeshVenueType(type, context));
        }
    });
}
exports.prepareModifyVenue = prepareModifyVenue;
/**
 * @hidden
 */
function getAuthorization({ venue: { id: venueId }, description, type }) {
    const transactions = [];
    if (description) {
        transactions.push(types_1.TxTags.settlement.UpdateVenueDetails);
    }
    if (type) {
        transactions.push(types_1.TxTags.settlement.UpdateVenueType);
    }
    return {
        roles: [{ type: types_1.RoleType.VenueOwner, venueId }],
        permissions: {
            tokens: [],
            portfolios: [],
            transactions,
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const modifyVenue = () => new internal_1.Procedure(prepareModifyVenue, getAuthorization);
exports.modifyVenue = modifyVenue;
//# sourceMappingURL=modifyVenue.js.map