"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getYamlViewValues = exports.getFormViewNetworkWideValues = exports.getFormViewHostsValues = exports.getFormData = exports.getStaticIpInfo = exports.getStaticIpInfoFromInfraEnvField = exports.getStaticNetworkConfig = void 0;
const nmstateYaml_1 = require("./nmstateYaml");
const dataTypes_1 = require("./dataTypes");
const dummyData_1 = require("./dummyData");
const formDataFromInfraEnvField_1 = require("./formDataFromInfraEnvField");
const emptyData_1 = require("./emptyData");
const utils_1 = require("../../../../../common/utils");
const getStaticNetworkConfig = (infraEnv) => {
    if (!infraEnv.staticNetworkConfig || !infraEnv.staticNetworkConfig.length) {
        return undefined;
    }
    const staticNetworkConfig = (0, utils_1.stringToJSON)(infraEnv.staticNetworkConfig);
    if (!staticNetworkConfig) {
        throw `Failed to parse static network config`;
    }
    if (!staticNetworkConfig.length) {
        return undefined;
    }
    return staticNetworkConfig;
};
exports.getStaticNetworkConfig = getStaticNetworkConfig;
const getView = (yamlComments) => {
    return yamlComments.includes(nmstateYaml_1.FORM_VIEW_PREFIX) ? dataTypes_1.StaticIpView.FORM : dataTypes_1.StaticIpView.YAML;
};
const getStaticIpInfoFromInfraEnvField = (staticNetworkConfig) => {
    const firstNetworkYaml = staticNetworkConfig[0].networkYaml;
    if (!firstNetworkYaml) {
        throw `Static Network Config doesn't contain a network yaml`;
    }
    const comments = (0, nmstateYaml_1.getYamlComments)(firstNetworkYaml);
    return {
        view: getView(comments),
        isDataComplete: !(0, dummyData_1.isDummyYaml)(firstNetworkYaml),
        formViewProtocolType: (0, nmstateYaml_1.getProtocolType)(comments),
    };
};
exports.getStaticIpInfoFromInfraEnvField = getStaticIpInfoFromInfraEnvField;
const getStaticIpInfo = (infraEnv) => {
    const staticNetworkConfig = (0, exports.getStaticNetworkConfig)(infraEnv);
    if (!staticNetworkConfig || !staticNetworkConfig[0]) {
        return undefined;
    }
    return (0, exports.getStaticIpInfoFromInfraEnvField)(staticNetworkConfig);
};
exports.getStaticIpInfo = getStaticIpInfo;
//Fails if form data doesn't exist
const getFormData = (infraEnv) => {
    const staticNetworkConfig = (0, exports.getStaticNetworkConfig)(infraEnv);
    if (!staticNetworkConfig) {
        throw `Infra env doesn't contain static ip data`;
    }
    const formData = (0, formDataFromInfraEnvField_1.formDataFromInfraEnvField)(staticNetworkConfig);
    if (!formData) {
        throw `Failed to get static ip form data from infra env ${infraEnv.id}`;
    }
    return formData;
};
exports.getFormData = getFormData;
const getFormViewHostsValues = (infraEnv) => {
    const formData = (0, exports.getFormData)(infraEnv);
    if (!formData.hosts.length) {
        return { hosts: [(0, emptyData_1.getEmptyFormViewHost)()] };
    }
    return { hosts: formData.hosts };
};
exports.getFormViewHostsValues = getFormViewHostsValues;
const getFormViewNetworkWideValues = (infraEnv) => {
    return (0, exports.getFormData)(infraEnv).networkWide;
};
exports.getFormViewNetworkWideValues = getFormViewNetworkWideValues;
const getYamlViewValues = (infraEnv) => {
    const staticNetworkConfig = (0, exports.getStaticNetworkConfig)(infraEnv);
    if (!staticNetworkConfig) {
        throw `Infra env doesn't contain static ip data`;
    }
    return { hosts: staticNetworkConfig };
};
exports.getYamlViewValues = getYamlViewValues;
//# sourceMappingURL=fromInfraEnv.js.map