"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const partiesToRaw = (parties) => parties.map((party) => [
    party.partyIdentifier,
    party.address,
    party.payList.map((payee) => {
        return [
            payee.address,
            payee.namespace,
            payee.instrument,
            payee.quantity,
            payee.publicKey || '',
            payee.signatature || '',
            payee.issuance || false,
            payee.metadata || '',
            payee.encumbrance || '',
        ];
    }),
    party.receiveList.map((recipient) => {
        return [
            recipient.address,
            recipient.namespace,
            recipient.instrument,
            recipient.quantity,
        ];
    }),
    '',
    '',
    !!party.mustSign,
    party.metadata,
]);
exports.contractToRaw = (contract) => {
    const partiesArray = partiesToRaw(contract.parties);
    const now = Math.floor(Date.now() / 1000) + 20;
    return {
        txtype: 'conew',
        walletid: contract.walletId,
        address: contract.address,
        contractdata: {
            __function: 'dvp_uk',
            parties: [
                contract.parties.length,
                ...partiesArray,
            ],
            authorisations: (contract.authorisations || []).map(auth => [
                auth.address,
                auth.authorisationId,
                auth.signature || '',
                auth.metadata,
                !!auth.refused,
            ]),
            parameters: (contract.parameters || []).map(param => [
                param.key,
                param.address,
                param.value,
                param.calculatedIndex,
                param.contractSpecific ? 1 : 0,
                param.calculationOnly ? 1 : 0,
                param.signature || '',
            ]),
            events: [
                'commit', 'expiry',
            ],
            expiry: now + contract.expirySeconds,
            startdate: contract.startDate || now,
            protocol: 'dvp',
            metadata: contract.metadata || '',
            addencumbrances: (contract.addencumbrance || []).map((enc) => {
                return [
                    enc.address,
                    enc.fullAssetId,
                    enc.reference,
                    enc.amount,
                    enc.beneficiaries.map(ben => [ben.address, ben.starttime, ben.endtime]),
                    enc.administrators.map(admin => [admin.address, admin.starttime, admin.endtime]),
                ];
            }),
            encumbrance: [!!contract.encumbrance, contract.encumbrance || ''],
            deletedelayoncomplete: contract.deleteDelay || 3600,
            autosign: contract.autoSign || true,
        },
    };
};
