"use strict";
/*
 *
 * Copyright 2018-present NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkCurrency = void 0;
const symbol_sdk_1 = require("symbol-sdk");
class NetworkCurrency {
    /**
     *Creates an instance of NetworkCurrency.
     * @param {NamespaceId} namespaceId
     * @param {number} divisibility
     */
    constructor(namespaceId, divisibility) {
        this.namespaceId = namespaceId;
        this.divisibility = divisibility;
    }
    /**
     * Creates a network currency from a network currency DTO
     * @static
     * @param {NetworkCurrencyDTO} networkCurrencyDTO
     * @returns {NetworkCurrency}
     */
    static createFromDTO(networkCurrencyDTO) {
        return new NetworkCurrency(new symbol_sdk_1.NamespaceId(networkCurrencyDTO.namespaceId), networkCurrencyDTO.divisibility);
    }
    /**
     * Creates a NetworkCurrency from the first block transactions
     * @static
     * @param {Transaction[]} transactions
     * @returns {NetworkCurrency}
     */
    static createFromFirstBlockTransactions(transactions) {
        // read first mosaic definition transaction
        const mosaicDefinitionTx = transactions.find((tx) => tx instanceof symbol_sdk_1.MosaicDefinitionTransaction);
        if (mosaicDefinitionTx === undefined) {
            throw new Error('mosaic definition transaction not found');
        }
        // read first root namespace
        const rootNamespaceTx = transactions.find((tx) => tx instanceof symbol_sdk_1.NamespaceRegistrationTransaction && tx.registrationType === symbol_sdk_1.NamespaceRegistrationType.RootNamespace);
        if (rootNamespaceTx === undefined) {
            throw new Error('root namespace creation transaction not found');
        }
        // read sub namespace
        const subNamespaceTx = transactions.find((tx) => tx instanceof symbol_sdk_1.NamespaceRegistrationTransaction &&
            tx.registrationType === symbol_sdk_1.NamespaceRegistrationType.SubNamespace &&
            tx.parentId &&
            tx.parentId.equals(rootNamespaceTx.namespaceId));
        if (rootNamespaceTx === undefined) {
            throw new Error('sub namespace creation transaction was not found');
        }
        // build network mosaic name
        const mosaicName = [rootNamespaceTx.namespaceName, subNamespaceTx.namespaceName].join('.');
        return new NetworkCurrency(new symbol_sdk_1.NamespaceId(mosaicName), mosaicDefinitionTx.divisibility);
    }
    /**
     * Creates a network currency DTO
     * @returns {NetworkCurrencyDTO}
     */
    toDTO() {
        const id = this.namespaceId.fullName ? this.namespaceId.fullName : this.namespaceId.toHex();
        return { namespaceId: id, divisibility: this.divisibility };
    }
    /**
     * Creates a mosaic given a relative amount
     * @param {(number | string)} amount
     * @returns {Mosaic}
     */
    createRelative(amount) {
        const theAmount = Number(amount);
        if (Number.isNaN(theAmount)) {
            throw new Error('The provided amount is invalid');
        }
        if (!(theAmount > 0)) {
            throw new Error('The provided amount should be greater than 0');
        }
        const absoluteAmount = theAmount * Math.pow(10, this.divisibility);
        return new symbol_sdk_1.Mosaic(this.namespaceId, symbol_sdk_1.UInt64.fromUint(absoluteAmount));
    }
}
exports.NetworkCurrency = NetworkCurrency;
