"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.MosaicService = void 0;
const symbol_sdk_1 = require("symbol-sdk");
/**
 * Mosaic service
 */
class MosaicService {
    /**
     * Validates a mosaic object from a string.
     * @param {string} value - Mosaic in the form mosaicId::amount.
     * @returns {true | string}
     */
    static validate(value) {
        let valid = true;
        const mosaicParts = value.split('::');
        try {
            if (isNaN(+mosaicParts[1])) {
                valid = false;
            }
            const ignored = new symbol_sdk_1.Mosaic(this.getMosaicId(mosaicParts[0]), symbol_sdk_1.UInt64.fromUint(+mosaicParts[1]));
        }
        catch (err) {
            valid = false;
        }
        return valid
            ? valid
            : 'Mosaic should be in the format (mosaicId(hex)|@aliasName)::absoluteAmount,' +
                ' (Ex: sending 1 symbol.xym, @symbol.xym::1000000)';
    }
    /**
     * Creates a MosaicId object from a string.
     * @param {string} rawMosaicId - Mosaic identifier. If starts with "@", it is a namespace name.
     * @returns {MosaicId | NamespaceId}
     */
    static getMosaicId(rawMosaicId) {
        let mosaicId;
        if (rawMosaicId.charAt(0) === MosaicService.ALIAS_TAG) {
            mosaicId = new symbol_sdk_1.NamespaceId(rawMosaicId.substring(1));
        }
        else {
            mosaicId = new symbol_sdk_1.MosaicId(rawMosaicId);
        }
        return mosaicId;
    }
    /**
     * Creates an array of mosaics from a string.
     * @param {string} rawMosaics - Mosaics in the form mosaicId::amount, separated by commas.
     * @returns {Mosaic[]}
     */
    static getMosaics(rawMosaics) {
        const mosaics = [];
        const mosaicsData = rawMosaics.split(',');
        mosaicsData.forEach((mosaicData) => {
            const mosaicParts = mosaicData.split('::');
            mosaics.push(new symbol_sdk_1.Mosaic(this.getMosaicId(mosaicParts[0]), symbol_sdk_1.UInt64.fromNumericString(mosaicParts[1])));
        });
        return mosaics;
    }
}
exports.MosaicService = MosaicService;
MosaicService.ALIAS_TAG = '@';
