"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const internal_attributes_1 = __importDefault(require("../processing/dom/internal-attributes"));
const form_data_entry_1 = __importDefault(require("./form-data-entry"));
const bufferUtils = __importStar(require("../utils/buffer"));
const json_1 = require("../utils/json");
const BOUNDARY_RE = /;\s*boundary=([^;]*)/i;
var ParserState;
(function (ParserState) {
    ParserState[ParserState["inPreamble"] = 0] = "inPreamble";
    ParserState[ParserState["inHeaders"] = 1] = "inHeaders";
    ParserState[ParserState["inBody"] = 2] = "inBody";
    ParserState[ParserState["inEpilogue"] = 3] = "inEpilogue";
})(ParserState || (ParserState = {}));
class FormData {
    constructor() {
        this.boundary = null;
        this._boundaryEnd = null;
        this._epilogue = [];
        this._entries = [];
        this._preamble = [];
    }
    _removeEntry(name) {
        this._entries = this._entries.filter(entry => entry.name !== name);
    }
    _injectFileInfo(fileInfo) {
        const entries = this._getEntriesByName(fileInfo.name);
        let previousEntry = null;
        for (let idx = 0; idx < fileInfo.files.length; idx++) {
            let entry = entries[idx];
            if (!entry) {
                entry = previousEntry ? previousEntry.cloneWithRawHeaders() : new form_data_entry_1.default();
                this._entries.push(entry);
            }
            previousEntry = entry;
            entry.addFileInfo(fileInfo, idx);
        }
    }
    _isBoundary(line) {
        return this.boundary.equals(line);
    }
    _isBoundaryEnd(line) {
        return this._boundaryEnd.equals(line);
    }
    _getEntriesByName(name) {
        return this._entries.reduce((found, entry) => {
            if (entry.name === name)
                found.push(entry);
            return found;
        }, []);
    }
    expandUploads() {
        const uploadsEntry = this._getEntriesByName(internal_attributes_1.default.uploadInfoHiddenInputName)[0];
        if (uploadsEntry) {
            const body = Buffer.concat(uploadsEntry.body).toString();
            const files = (0, json_1.parse)(body);
            this._removeEntry(internal_attributes_1.default.uploadInfoHiddenInputName);
            files.forEach(fileInfo => this._injectFileInfo(fileInfo));
        }
    }
    parseContentTypeHeader(header) {
        header = String(header);
        if (header.includes('multipart/form-data')) {
            const boundaryMatch = header.match(BOUNDARY_RE);
            const token = boundaryMatch && boundaryMatch[1];
            if (token) {
                this.boundary = Buffer.from('--' + token);
                this._boundaryEnd = Buffer.from('--' + token + '--');
            }
        }
    }
    parseBody(body) {
        let state = ParserState.inPreamble;
        const lines = bufferUtils.createLineIterator(body);
        let currentEntry = null;
        for (const line of lines) {
            if (this._isBoundary(line)) {
                if (currentEntry)
                    this._entries.push(currentEntry);
                state = ParserState.inHeaders;
                currentEntry = new form_data_entry_1.default();
            }
            else if (this._isBoundaryEnd(line)) {
                if (currentEntry)
                    this._entries.push(currentEntry);
                state = ParserState.inEpilogue;
            }
            else if (state === ParserState.inPreamble)
                bufferUtils.appendLine(this._preamble, line);
            else if (state === ParserState.inHeaders) {
                if (line.length)
                    currentEntry === null || currentEntry === void 0 ? void 0 : currentEntry.setRawHeader(line.toString()); // eslint-disable-line no-unused-expressions
                else
                    state = ParserState.inBody;
            }
            else if (state === ParserState.inEpilogue)
                bufferUtils.appendLine(this._epilogue, line);
            else if (state === ParserState.inBody && currentEntry)
                bufferUtils.appendLine(currentEntry.body, line);
        }
    }
    toBuffer() {
        if (!this._boundaryEnd || !this.boundary)
            return null;
        let chunks = this._preamble;
        if (chunks.length)
            chunks.push(bufferUtils.CRLF);
        for (const entry of this._entries) {
            chunks.push(this.boundary, bufferUtils.CRLF, entry.toBuffer(), bufferUtils.CRLF);
        }
        chunks.push(this._boundaryEnd, bufferUtils.CRLF);
        chunks = chunks.concat(this._epilogue);
        return Buffer.concat(chunks);
    }
}
exports.default = FormData;module.exports = exports.default;