<commit_before>import { useGetAddressKeys } from 'react-components';
import {
    getCreateCalendarEvent,
    getSwitchCalendarEvent,
    getUpdateCalendarEvent,
} from 'proton-shared/lib/calendar/integration/createOrUpdateEvent';
import { updateCalendar } from 'proton-shared/lib/api/calendars';
import { createCalendarEvent } from 'proton-shared/lib/calendar/serialize';
import { Api } from 'proton-shared/lib/interfaces';
import { Calendar } from 'proton-shared/lib/interfaces/calendar';
import getCreationKeys from '../getCreationKeys';
import { EventOldData, EventNewData } from '../../../interfaces/EventData';

interface SaveEventHelperArguments {
    oldEditEventData?: EventOldData;
    newEditEventData: EventNewData;

    api: Api;
    getAddressKeys: ReturnType<typeof useGetAddressKeys>;
    getCalendarKeys: ReturnType<typeof useGetAddressKeys>;
    calendars: Calendar[];
}

const handleSaveSingleEventHelper = async ({
    oldEditEventData,
    newEditEventData: {
        calendarID: newCalendarID,
        addressID: newAddressID,
        memberID: newMemberID,
        veventComponent: newVeventComponent,
    },
    api,
    getAddressKeys,
    getCalendarKeys,
    calendars,
}: SaveEventHelperArguments) => {
    const oldEvent = oldEditEventData?.eventData;
    const oldCalendarID = oldEditEventData?.calendarID;

    const isUpdateEvent = !!oldEvent;
    const isSwitchCalendar = isUpdateEvent && oldCalendarID !== newCalendarID;

    const [addressKeys, newCalendarKeys, oldCalendarKeys] = await Promise.all([
        getAddressKeys(newAddressID),
        getCalendarKeys(newCalendarID),
        isSwitchCalendar && oldCalendarID ? getCalendarKeys(oldCalendarID) : undefined,
    ]);
    const data = await createCalendarEvent({
        eventComponent: newVeventComponent,
        isSwitchCalendar,
        ...(await getCreationKeys({ Event: oldEvent, addressKeys, newCalendarKeys, oldCalendarKeys })),
    });

    let result;
    if (isSwitchCalendar) {
        result = await api(
            getSwitchCalendarEvent({
                calendarID: newCalendarID,
                memberID: newMemberID,
                data,
                UID: newVeventComponent.uid.value,
            })
        );
    } else if (isUpdateEvent) {
        result = await api(getUpdateCalendarEvent({ memberID: newMemberID, data, Event: oldEvent }));
    } else {
        result = await api(getCreateCalendarEvent({ calendarID: newCalendarID, memberID: newMemberID, data }));
    }

    const calendar = calendars.find(({ ID }) => ID === newCalendarID);
    if (calendar && !calendar.Display) {
        await api(updateCalendar(newCalendarID, { Display: 1 }));
    }

    return result;
};

export default handleSaveSingleEventHelper;<commit_msg>Fix tsc<commit_after>import { useGetAddressKeys } from 'react-components';
import {
    getCreateCalendarEvent,
    getSwitchCalendarEvent,
    getUpdateCalendarEvent,
} from 'proton-shared/lib/calendar/integration/createOrUpdateEvent';
import { updateCalendar } from 'proton-shared/lib/api/calendars';
import { createCalendarEvent } from 'proton-shared/lib/calendar/serialize';
import { Api } from 'proton-shared/lib/interfaces';
import { Calendar } from 'proton-shared/lib/interfaces/calendar';
import getCreationKeys from '../getCreationKeys';
import { EventOldData, EventNewData } from '../../../interfaces/EventData';

interface SaveEventHelperArguments {
    oldEditEventData?: EventOldData;
    newEditEventData: EventNewData;

    api: Api;
    getAddressKeys: ReturnType<typeof useGetAddressKeys>;
    getCalendarKeys: ReturnType<typeof useGetAddressKeys>;
    calendars: Calendar[];
}

const handleSaveSingleEventHelper = async ({
    oldEditEventData,
    newEditEventData: {
        calendarID: newCalendarID,
        addressID: newAddressID,
        memberID: newMemberID,
        veventComponent: newVeventComponent,
    },
    api,
    getAddressKeys,
    getCalendarKeys,
    calendars,
}: SaveEventHelperArguments) => {
    const oldEvent = oldEditEventData?.eventData;
    const oldCalendarID = oldEditEventData?.calendarID;

    const isUpdateEvent = !!oldEvent;
    const isSwitchCalendar = isUpdateEvent && oldCalendarID !== newCalendarID;

    const [addressKeys, newCalendarKeys, oldCalendarKeys] = await Promise.all([
        getAddressKeys(newAddressID),
        getCalendarKeys(newCalendarID),
        isSwitchCalendar && oldCalendarID ? getCalendarKeys(oldCalendarID) : undefined,
    ]);
    const data = await createCalendarEvent({
        eventComponent: newVeventComponent,
        isSwitchCalendar,
        ...(await getCreationKeys({ Event: oldEvent, addressKeys, newCalendarKeys, oldCalendarKeys })),
    });

    let result;
    if (isSwitchCalendar) {
        result = await api(
            getSwitchCalendarEvent({
                calendarID: newCalendarID,
                memberID: newMemberID,
                data,
                UID: newVeventComponent.uid.value,
            })
        );
    } else if (isUpdateEvent && oldEvent) {
        result = await api(getUpdateCalendarEvent({ memberID: newMemberID, data, Event: oldEvent }));
    } else {
        result = await api(getCreateCalendarEvent({ calendarID: newCalendarID, memberID: newMemberID, data }));
    }

    const calendar = calendars.find(({ ID }) => ID === newCalendarID);
    if (calendar && !calendar.Display) {
        await api(updateCalendar(newCalendarID, { Display: 1 }));
    }

    return result;
};

export default handleSaveSingleEventHelper;
{'fraction_non_alphanumeric': 0.10144927536231885, 'fraction_numerical': 0.00036231884057971015, 'mean_word_length': 3.481331168831169, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337532', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1498, 'n_words': 325}