"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRouteConfig = exports.getInterface = exports.getDnsSection = exports.getNmstateProtocolConfig = exports.getProtocolType = exports.getMachineNetworks = exports.toYamlWithComments = exports.getYamlComments = exports.yamlToNmstateObject = exports.getProtocolVersionIdx = exports.getVlanNicName = exports.getMachineNetworkFieldName = exports.FORM_VIEW_PREFIX = exports.YAML_COMMENT_CHAR = void 0;
const tslib_1 = require("tslib");
const js_yaml_1 = require("js-yaml");
const nmstateTypes_1 = require("./nmstateTypes");
const dataTypes_1 = require("./dataTypes");
const findLastIndex_js_1 = tslib_1.__importDefault(require("lodash-es/findLastIndex.js"));
const REAL_NIC_NAME = 'eth0';
const REAL_NIC_NAME_1 = 'eth1';
const ROUTE_DESTINATIONS = {
    ipv4: '0.0.0.0/0',
    ipv6: '::/0',
};
const TABLE_ID = 254;
exports.YAML_COMMENT_CHAR = '#';
exports.FORM_VIEW_PREFIX = 'generated by ui form view';
const getMachineNetworkFieldName = (protocolVersion) => {
    return `${protocolVersion}MachineNetwork`;
};
exports.getMachineNetworkFieldName = getMachineNetworkFieldName;
const getVlanNicName = (nicName, vlanId) => `${nicName}.${vlanId}`;
exports.getVlanNicName = getVlanNicName;
const getProtocolVersionIdx = (protocolVersion) => {
    return protocolVersion === dataTypes_1.ProtocolVersion.ipv4 ? 0 : 1;
};
exports.getProtocolVersionIdx = getProtocolVersionIdx;
const yamlToNmstateObject = (yaml) => {
    return (0, js_yaml_1.load)(yaml);
};
exports.yamlToNmstateObject = yamlToNmstateObject;
const getYamlComments = (yaml) => {
    const lines = yaml.split('\n');
    const lastCommentIdx = (0, findLastIndex_js_1.default)(lines, (line) => line.startsWith(exports.YAML_COMMENT_CHAR));
    return lines.slice(0, lastCommentIdx + 1).map((line) => line.slice(1));
};
exports.getYamlComments = getYamlComments;
const toYamlWithComments = (json, comments) => {
    const yamlComments = comments.map((comment) => `${exports.YAML_COMMENT_CHAR}${comment}`);
    return `${yamlComments.join('\n')}\n${(0, js_yaml_1.dump)(json)}`;
};
exports.toYamlWithComments = toYamlWithComments;
const getMachineNetwork = (comments, protocolVersion) => {
    const line = comments.find((comment) => comment.startsWith((0, exports.getMachineNetworkFieldName)(protocolVersion)));
    return line ? line.split(' ')[1] : '';
};
const getMachineNetworks = (comments) => {
    return {
        ipv4: getMachineNetwork(comments, dataTypes_1.ProtocolVersion.ipv4),
        ipv6: getMachineNetwork(comments, dataTypes_1.ProtocolVersion.ipv6),
    };
};
exports.getMachineNetworks = getMachineNetworks;
const getProtocolType = (comments) => {
    const machineNetworks = (0, exports.getMachineNetworks)(comments);
    if (machineNetworks[dataTypes_1.ProtocolVersion.ipv4] && machineNetworks[dataTypes_1.ProtocolVersion.ipv6]) {
        return 'dualStack';
    }
    if (machineNetworks[dataTypes_1.ProtocolVersion.ipv4]) {
        return 'ipv4';
    }
    return null;
};
exports.getProtocolType = getProtocolType;
const getNmstateProtocolConfig = (ipAddress, prefixLength, protocolVersion) => {
    return Object.assign({ address: [
            {
                ip: ipAddress,
                'prefix-length': prefixLength,
            },
        ], enabled: true, dhcp: false }, (protocolVersion === dataTypes_1.ProtocolVersion.ipv6 && { autoconf: false }));
};
exports.getNmstateProtocolConfig = getNmstateProtocolConfig;
const getDnsSection = (dns) => {
    const dnsServers = dns.split(',');
    return { config: { server: dnsServers } };
};
exports.getDnsSection = getDnsSection;
const getInterface = (nicName, protocolConfigs, networkWide, bondType, hasBondsConfigured, createBondInterface) => {
    if (networkWide.useVlan && networkWide.vlanId && !createBondInterface) {
        return Object.assign({ name: (0, exports.getVlanNicName)(nicName, networkWide.vlanId), type: nmstateTypes_1.NmstateInterfaceType.VLAN, state: 'up', vlan: { 'base-iface': nicName, id: networkWide.vlanId } }, protocolConfigs);
    }
    else if (bondType && hasBondsConfigured) {
        return Object.assign(Object.assign({ name: nicName, type: nmstateTypes_1.NmstateInterfaceType.BOND, state: 'up' }, (networkWide.useVlan ? {} : protocolConfigs)), { 'link-aggregation': {
                mode: bondType,
                options: {
                    miimon: '100',
                },
                port: [REAL_NIC_NAME, REAL_NIC_NAME_1],
            } });
    }
    else {
        return Object.assign({ name: nicName, type: nmstateTypes_1.NmstateInterfaceType.ETHERNET, state: 'up' }, protocolConfigs);
    }
};
exports.getInterface = getInterface;
const getRouteConfig = (protocolVersion, gatewayAddress, nicName) => {
    return {
        destination: ROUTE_DESTINATIONS[protocolVersion],
        'next-hop-address': gatewayAddress,
        'next-hop-interface': nicName,
        'table-id': TABLE_ID,
    };
};
exports.getRouteConfig = getRouteConfig;
//# sourceMappingURL=nmstateYaml.js.map