"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("./api");
const contract_1 = require("./contract");
const network_1 = require("@gny/network");
function isIpOrUrl(value) {
    const ip = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    const url = /^([a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)+.*)$/;
    return ip.test(value) || url.test(value);
}
function isPort(value) {
    const result = Number.isInteger(value) && value > 0 && value < 65535;
    return result;
}
function isNetworkType(value) {
    if (value === 'localnet' ||
        value === 'testnet' ||
        value === 'testnet_app' ||
        value === 'mainnet') {
        return true;
    }
    return false;
}
function isBoolean(value) {
    const result = typeof value === 'boolean';
    return result;
}
class Connection {
    constructor(host, port, network, https = false) {
        this.api = api_1.Api(this);
        this.contract = contract_1.Contract(this);
        this.host = host || '127.0.0.1';
        this.port = port || 4096;
        this.network = network || 'localnet';
        if (this.port === 80) {
            this.baseUrl = `${https ? 'https' : 'http'}://${this.host}`;
        }
        else {
            this.baseUrl = `${https ? 'https' : 'http'}://${this.host}:${this.port}`;
        }
        if (!isIpOrUrl(this.host)) {
            throw new Error('host not valid');
        }
        if (!isPort(this.port)) {
            throw new Error('port not valid');
        }
        if (!isNetworkType(this.network)) {
            throw new Error('networktype not valid');
        }
        this.hash = network_1.getConfig(this.network).hash;
        if (!isBoolean(https)) {
            throw new Error('https not valid');
        }
    }
}
exports.Connection = Connection;
//# sourceMappingURL=connection.js.map