"use strict";
// @ts-nocheck
// SwmmParse.tsx
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwmmParse = void 0;
/**
* Class for translating .inp files between .txt and JSON format.
* See SwmmInp for methods related to working with .inp files in JSON format.
* This class will also be used for BSON translations, soon...
* This class is intended to provide purely static functions. See SwmmInp for
* JSON formatted inp object storage and operations.
*/
class SwmmParse {
    /**
    * Constructor for the SwmmParse class.
    */
    constructor() { }
    /**
     * Takes a line of text, usually parsed from a .inp file, and splits into different values.
     *
     * @param text line of text from a .inp file.
     * @returns array of values in a line from a .inp file.
     */
    static splitDataLine(text) {
        // Split the input string by whitespace
        let tokens = text.trim().match(/(?:"[^"]*"|[^\s]+)/g);
        // Initialize an empty array to store the parsed tokens
        let parsedTokens = [];
        // Loop through each token
        for (let token of tokens) {
            // Remove quotes from the token if it's a quoted string
            if (token.startsWith('"') && token.endsWith('"')) {
                token = token.slice(1, -1);
            }
            // Add the token to the parsed tokens array
            parsedTokens.push(token);
        }
        return parsedTokens;
    }
    /**
     * Takes a line of text, usually parsed from a .inp file, and translates to a PCS object.
     * The PCS object can then be added to an EPA-SWMM JSON model.
     *
     * @param text line of text from a .inp file.
     * @returns JSON formatted verion of .inp file.
     */
    static parseLine_PCS(text) {
        const array = this.splitDataLine(text);
        // Planar Coordinate System data for transforming to lat/lon values
        if (array && array.length === 6) {
            return {
                [array[0]]: {
                    proj: array[1],
                    zone: array[2],
                    datum: array[3],
                    units: array[4],
                    defs: array[5]
                }
            };
        }
        else {
            throw new Error('PCS line could not be parsed.');
        }
    }
    static parseLine_TRANSFORM(text) {
        const array = this.splitDataLine(text);
        // Transform EPA-SWMM coordinates for visual objects
        if (array && array.length === 5) {
            return {
                [array[0]]: {
                    x: parseFloat(array[1]),
                    y: parseFloat(array[2]),
                    size: parseFloat(array[3]),
                    rotation: parseFloat(array[4])
                }
            };
        }
        else {
            throw new Error('TRANSFORM line could not be parsed.');
        }
    }
    static parseLine_OPTIONS(text) {
        const array = this.splitDataLine(text);
        // Transform EPA-SWMM OPTIONS section
        if (array && array.length > 1) {
            return {
                [array[0]]: array[1].toUpperCase()
            };
        }
        else {
            throw new Error('OPTIONS line could not be parsed.');
        }
    }
    static parseLine_RAINGAGES(text) {
        const array = this.splitDataLine(text);
        // Transform EPA-SWMM RAINGAGES section
        if (array && (array.length === 6 || array.length === 5)) {
            // Use a timeseries format for shorter elements.
            return {
                [array[0]]: {
                    Format: array[1],
                    Interval: array[2],
                    SCF: array[3],
                    Source: array[4],
                    SeriesName: array[5] ? array[5] : ''
                }
            };
        }
        else if (array && array.length === 8) {
            // Use a file format for longer elements.
            return {
                [array[0]]: {
                    Format: array[1],
                    Interval: array[2],
                    SCF: array[3],
                    Source: array[4],
                    Fname: array[5].replaceAll('"', ''),
                    Station: array[6],
                    Units: array[7]
                }
            };
        }
        else {
            throw new Error('RAINGAGES line could not be parsed.');
        }
    }
    static parseLine_TEMPERATURE(text) {
        const array = this.splitDataLine(text);
        let obj = {};
        if (array && array.length) {
            switch (array[0]) {
                case 'TIMESERIES':
                    obj[array[0]] = array[1].trim();
                    break;
                case 'FILE':
                    obj[array[0]] = array[1].trim();
                    obj.Start = array[2] ? array[2] === '*' ? '' : array[2] : '';
                    obj.Units = array[3] ? array[3] : '';
                    break;
                case 'WINDSPEED':
                    switch (array[1].trim()) {
                        case 'MONTHLY':
                            // Read in 12 numbers
                            obj[array[0]] = { Type: 'MONTHLY', AWS: [] };
                            for (let i = 0; i < 12; i++) {
                                obj[array[0]].AWS[i] = parseFloat(array[i + 2]);
                            }
                            break;
                        case 'FILE':
                            // Actual file name is in model.TEMPERATURE.File
                            obj[array[0]] = { Type: 'FILE' };
                            break;
                    }
                    break;
                case 'SNOWMELT':
                    obj[array[0]] = {
                        DivideTemp: parseFloat(array[1]),
                        ATIWeight: parseFloat(array[2]),
                        NegMeltRatio: parseFloat(array[3]),
                        MSLElev: parseFloat(array[4]),
                        DegLatitude: parseFloat(array[5]),
                        LongCorrection: parseFloat(array[6])
                    };
                    break;
                case 'ADC':
                    obj[array[0]] = {};
                    obj[array[0]][array[1]] = [];
                    for (let i = 0; i < 10; i++) {
                        obj[array[0]][array[1]][i] = parseFloat(array[i + 2]);
                    }
                    break;
                default:
                    throw new Error('TEMPERATURE line could not be parsed.');
            }
            return obj;
        }
        else {
            throw new Error('TEMPERATURE line could not be parsed.');
        }
    }
    static parseLine_ADJUSTMENTS(text) {
        const array = this.splitDataLine(text);
        if (array && (array.length === 3 || array.length === 13)) {
            // If this is just a 12-number array
            let obj = {
                MONTHLY: {},
                PATTERN: {}
            };
            // Read in 12 numbers or some strings
            if (['TEMPERATURE', 'EVAPORATION', 'RAINFALL', 'CONDUCTIVITY'].includes(array[0])) {
                obj.MONTHLY[array[0]] = [];
                for (let i = 0; i < array.length - 1; i++) {
                    obj.MONTHLY[array[0]][i] = array[i + 1];
                }
            }
            // If this is a subcatchment-associated pattern group
            else {
                obj.PATTERN[array[1]] = {
                    "DSTORE": '',
                    "N-PERV": '',
                    "INFIL": ''
                };
                obj.PATTERN[array[1]][array[0]] = array[2];
            }
            return obj;
        }
        else {
            throw new Error('ADJUSTMENTS line could not be parsed.');
        }
    }
}
exports.SwmmParse = SwmmParse;
function isValidData(data) {
    if (typeof data !== 'undefined' && data !== null)
        return true;
    return false;
}
