function simpleParseHeaders (packet) {
    return Object.fromEntries(packet.match(/[a-z\-]{1,}:\s{0,}.{1,}(\n|\r){1,}/gmiu)?.map(i => {
        const [key, value] = i.split(/:\s{0,}/miu);
        return [
            key.trim(),
            value.trim()
        ];
    }) ?? []);
}

class PacketParseResult {
    constructor (result, isEncrypted = false) {
        this.result = result;
        this.isEncrypted = isEncrypted;
    }
}

module.exports = async (bytes) => {
    console.log("((('", bytes.toString("utf8"), "')))");
    const packet = bytes.toString("utf8");

    const isHTTP = packet.match(/(GET|POST|PUT|DELETE|OPTIONS|HEAD|PATCH|TRACE|CONNECT)\s{1,}\/[a-zA-Z0-9_\-\.\/?=&%:]{0,}\s{1,}HTTP(S|)\/(1\.0|1\.1|2\.0)(\n|\r)/gmu)?.length > 0 ?? false;
	//console.log({ isHTTP });
    let isWebSocket = false;
    if (isHTTP) {
        const headers = simpleParseHeaders(packet);
        if (headers.Upgrade === "websocket")
            isWebSocket = true;
    }
    return new PacketParseResult({
        isHTTP, isWebSocket
    }, false);
}
