1 import { c } from 'ttag';
3 import type { useGetAddressKeys } from '@proton/components';
5 import { createCalendar, updateCalendarUserSettings } from '../../../api/calendars';
6 import { getRandomAccentColor } from '../../../colors';
7 import { getTimezone } from '../../../date/timezone';
8 import { getActiveAddresses } from '../../../helpers/address';
9 import type { Address, Api } from '../../../interfaces';
10 import type { CalendarWithOwnMembers } from '../../../interfaces/calendar';
11 import { getPrimaryKey } from '../../../keys';
12 import { DEFAULT_CALENDAR } from '../../constants';
13 import { setupCalendarKey } from './setupCalendarKeys';
18 getAddressKeys: ReturnType<typeof useGetAddressKeys>;
21 const setupCalendarHelper = async ({ addresses, api, getAddressKeys }: Args) => {
22 const activeAddresses = getActiveAddresses(addresses);
23 if (!activeAddresses.length) {
24 throw new Error(c('Error').t`No valid address found`);
27 const [{ ID: addressID }] = activeAddresses;
28 const { privateKey: primaryAddressKey } = getPrimaryKey(await getAddressKeys(addressID)) || {};
29 if (!primaryAddressKey) {
30 throw new Error(c('Error').t`Primary address key is not decrypted.`);
33 const { Calendar } = await api<{ Calendar: CalendarWithOwnMembers }>(
35 Name: DEFAULT_CALENDAR.name,
36 Color: getRandomAccentColor(),
37 Description: DEFAULT_CALENDAR.description,
42 const updatedCalendarUserSettings = {
43 PrimaryTimezone: getTimezone(),
44 AutoDetectPrimaryTimezone: 1,
49 Passphrase: { Flags },
51 ] = await Promise.all([
54 calendarID: Calendar.ID,
58 api(updateCalendarUserSettings(updatedCalendarUserSettings)),
61 // There is only one member in the calendar at this point
62 const calendarWithUpdatedFlags = {
66 ...Calendar.Members[0],
73 calendar: calendarWithUpdatedFlags,
74 updatedCalendarUserSettings,
78 export default setupCalendarHelper;