import { fetchUserAbilities } from "@/app/utils/fetchUtil";
import MailSetting from "./MailSetting";
import MailSettingLoading from "./MailSettingLoading";
import { MailTemplate, fetchMailSetting, fetchMailTimesheetTemplate } from "@/app/api/mail";

interface SubComponents {
    Loading: typeof MailSettingLoading;
}

const MailSettingWrapper: React.FC & SubComponents = async () => {
    const [
        abilities,
        settings,
        timesheetTemplate,
    ] = await Promise.all([
        fetchUserAbilities(),
        fetchMailSetting(),
        fetchMailTimesheetTemplate()
    ]);

    const tempTimesheetTemplate: MailTemplate = {
        cc: timesheetTemplate.find(template => template.name.includes(".cc"))?.value,
        bcc: timesheetTemplate.find(template => template.name.includes(".bcc"))?.value,
        subject: timesheetTemplate.find(template => template.name.includes(".subject"))?.value,
        template: timesheetTemplate.find(template => template.name.includes(".template"))?.value,
    }
    return (
        <MailSetting
            defaultInputs={{
                settings: settings,
                template: tempTimesheetTemplate,
            }}
        />
    );
};

MailSettingWrapper.Loading = MailSettingLoading;


export default MailSettingWrapper;
