// const logger = require("../utils/log.cjs")
const config = require("../utils/config.cjs");
const request = require("request");
const CryptoJS = require("crypto-js");

module.exports = {
    config: async () => {
        return config;
    },
    getEnvByKey:async (req,res)=>{
        const query = req.query;
        const key = query.key;
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.end(process.env[key])
    },
    downloadWithFileName: async (req, res) => {
        const query = req.query;
        const response = request(decodeURIComponent(query.url), {
            headers: query.headers,
        });
        response.on("response", (resp) => {
            ["content-length", "content-type"].forEach(
                (key) => {
                    if (resp.headers[key]) {
                        res.setHeader(key, resp.headers[key]);
                    }
                }
            );
            let disposition;
            if(query.filename){
                const filename = encodeURIComponent(query.filename);
                disposition = `attachment; filename="${filename}"`;
            }else {
                disposition = resp.headers["content-dispositions"] || ""
            }
            res.setHeader("content-disposition", disposition);
            res.setHeader("access-control-allow-origin", "*")
        });

        response.on("error", (e) => {
            res.status(500).send(String(e));
        });
        response.on("end", () => {
            res.end();
        });
        response.on("data", (chunk) => {
            res.write(chunk);
        });
    },
    download: async (req, res) => {
        const query = req.query;
        query.url = CryptoJS.enc.Utf8.stringify(
            CryptoJS.enc.Base64.parse(query.url)
        ).toString();
        query.headers = {
            "User-Agent":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0",
            Accept: "*/*",
            "Accept-Language":
                "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            Connection: "keep-alive",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "cross-site",
            ...JSON.parse(
                CryptoJS.enc.Utf8.stringify(
                    CryptoJS.enc.Base64.parse(query.headers)
                ).toString()
            ),
        };
        const response = request(query.url, {
            headers: query.headers,
        });
        response.on("response", (resp) => {
            ["content-length", "content-type", "content-disposition"].forEach(
                (key) => {
                    if (resp.headers[key]) {
                        res.setHeader(key, resp.headers[key]);
                    }
                }
            );
            res.setHeader("access-control-allow-origin", "*")
        });

        response.on("error", (e) => {
            res.status(500).send(String(e));
        });
        response.on("end", () => {
            res.end();
        });
        response.on("data", (chunk) => {
            res.write(chunk);
        });
        // response.pipe(res);
    },
};
