import fs from "fs";
import path from "path";

export default function getHeader(
    companyId: string,
    header: string | null | undefined,
    fallback: string
): string {
    const rootPath = process.env.PUBLIC_HEADER_URL;
    if (companyId == null) return fallback;
    if (header == undefined || header == null || header === "") return fallback;

    const rootDir =
        process.env.NODE_ENV !== "production"
            ? process.env.GET_HEADER_COMPANIES_FILES_ROOT_DEV
            : process.env.GET_HEADER_COMPANIES_FILES_ROOT_PRO;

    const uploadPath = path.join(__dirname, rootDir + companyId);

    const filePath = path.join(uploadPath, header);

    if (!fs.existsSync(filePath)) {
        return fallback;
    }

    return header ? rootPath + companyId + "/" + header : "";
}
