"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.event = event; exports.todo = todo; exports.journal = journal; exports.alarm = alarm; const mime_types_1 = require("mime-types"); const BR = '\r\n'; function dateWithUTCTime(now) { const year = now.getUTCFullYear(); const month = (now.getUTCMonth() + 1).toString().padStart(2, '0'); const day = now.getUTCDate().toString().padStart(2, '0'); const hours = now.getUTCHours().toString().padStart(2, '0'); const minutes = now.getUTCMinutes().toString().padStart(2, '0'); const seconds = now.getUTCSeconds().toString().padStart(2, '0'); return `${year}${month}${day}T${hours}${minutes}${seconds}Z`; } function unfolding(str, maxLimit = 75) { const length = str.length; let outStr = ''; if (length < maxLimit) { return str; } for (let i = 0, j = 2; i < length; i += maxLimit % length, j += 1) { outStr += str.slice(i, i + maxLimit) + '\n' + ' '.repeat(j); } return outStr; } function createEmail(str) { const MAILTO = 'mailto:'; return str.startsWith(MAILTO) ? str : MAILTO + str; } function recurrenceRule({ freq, interval, count, until, wkst = 'MO', byday, byweekno, bymonthday, byyearday, }) { let outStr = ''; if (freq) { outStr += 'FREQ=' + freq + ';'; } if (interval) { outStr += 'INTERVAL=' + interval + ';'; } if (count) { outStr += 'COUNT=' + count + ';'; } if (until) { outStr += 'UNTIL=' + until + ';'; } outStr += wkst + ';'; if (byday) { outStr += 'BYDAY='; if (Array.isArray(byday)) { outStr += byday.join(','); } else { outStr += byday; } outStr += ';'; } if (byweekno) { outStr += 'BYWEEKNO' + byweekno + ';'; } if (bymonthday) { outStr += 'BYMONTHDAY='; if (Array.isArray(bymonthday)) { outStr += bymonthday.join(','); } else { outStr += bymonthday; } outStr += ';'; } if (byyearday) { outStr += 'BYYEARDAY' + byyearday + ';'; } return outStr; } function createOrganizer(organizer) { let str = ''; if (Array.isArray(organizer)) { for (const address of organizer) { let org = 'ORGANIZER;'; org += 'CN=' + address.name; if (address.email) { org += `:${createEmail(address.email)}`; } str += org + BR; } } else { str += `ORGANIZER:${organizer}` + BR; } return str; } function createUri(url) { return 'URL;VALUE=URI:' + url.toString(); } function createClass(klass) { return 'CLASS:' + klass.toUpperCase(); } function createTransp(transp) { return 'TRANSP:' + transp.toUpperCase(); } function createAttach(base64) { const [type, temp] = base64.split('data:')[1].split(';'); const [encoding, data] = temp.split(','); let str = 'ATTACH'; str += ';FMTTYPE=' + type; str += ';FILENAME=' + globalThis.crypto.randomUUID() + '.' + (0, mime_types_1.extension)(type); str += ';ENCODING=' + encoding.toUpperCase(); str += ';VALUE=' + 'BINARY'; str += ':' + data; return str; } function event({ uid, location, geo, summary, description, stamp, start, end, attach, organizer, attendee, url, status, categories, rrule, klass, transp, }) { let str = 'BEGIN:VEVENT' + BR; str += `UID:${uid}` + BR; if (stamp instanceof Date) { str += `DTSTAMP:${dateWithUTCTime(stamp)}` + BR; } if (start instanceof Date) { str += `DTSTART:${dateWithUTCTime(start)}` + BR; } if (end instanceof Date) { str += `DTEND:${dateWithUTCTime(end)}` + BR; } if (location?.length) { str += `LOCATION:${location}` + BR; } if (geo && Array.isArray(geo)) { str += `GEO:${geo[0]};${geo[1]}` + BR; } if (summary?.length) { str += `SUMMARY:${unfolding(summary)}` + BR; } if (description?.length) { str += `DESCRIPTION:${(description)}` + BR; } if (status?.length) { str += `STATUS:${status}` + BR; } if (categories) { str += `CATEGORIES:${categories}` + BR; } if (organizer) { str += createOrganizer(organizer); } if (attendee) { str += createOrganizer(attendee); } if (attach) { if (Array.isArray(attach)) { for (const base64 of attach) { str += createAttach(base64) + BR; } } else { str += createAttach(attach) + BR; } } if (url && url instanceof URL) { str += createUri(url) + BR; } if (klass) { str += createClass(klass) + BR; } if (transp) { str += createTransp(transp) + BR; } if (rrule) { str += 'RRULE:' + recurrenceRule(rrule) + BR; } str += 'END:VEVENT'; return str; } function todo({ uid, stamp, due, summary, description, priority, status, rrule, }) { let str = 'BEGIN:VTODO' + BR; str += 'UID:' + uid + BR; if (stamp instanceof Date) { str += `DTSTAMP:${dateWithUTCTime(stamp)}` + BR; } if (due instanceof Date) { str += `DTSTAMP:${dateWithUTCTime(due)}` + BR; } if (summary?.length) { str += 'SUMMARY:' + unfolding(summary) + BR; } if (description?.length) { str += 'DESCRIPTION:' + (description) + BR; } if (priority) { str += 'PRIORITY:' + String(priority) + BR; } if (status?.length) { str += 'STATUS:' + status + BR; } if (rrule) { str += 'RRULE:' + recurrenceRule(rrule) + BR; } str += 'END:VTODO'; return str; } function journal({ uid, stamp, start, summary, description, rrule, }) { let str = 'BEGIN:VJOURNAL'; str += `UID:${uid}` + BR; if (stamp instanceof Date) { str += `DTSTAMP:${dateWithUTCTime(stamp)}` + BR; } if (start instanceof Date) { str += `DTSTART:${dateWithUTCTime(start)}` + BR; } if (summary?.length) { str += `SUMMARY:${unfolding(summary)}` + BR; } if (description?.length) { str += `DESCRIPTION:${(description)}` + BR; } if (rrule) { str += 'RRULE:' + recurrenceRule(rrule) + BR; } str += 'END:VJOURNAL'; return str; } function alarm({ uid, action, description, trigger }) { let str = 'BEGIN:VALARM' + BR; str += 'UID:' + uid + BR; if (trigger) { str += 'TRIGGER:' + trigger + BR; } if (description?.length) { str += 'DESCRIPTION:' + description + BR; } if (action) { str += 'ACTION:' + action + BR; } str += 'END:VALARM'; return str; } exports.default = (id, { event, todo, journal, alarm, }) => { let str = 'BEGIN:VCALENDAR' + BR; str += 'VERSION:2.0' + BR; str += 'PRODID:' + id + BR; str += 'CALSCALE:GREGORIAN' + BR; str += 'METHOD:PUBLISH' + BR; if (event) { str += event + BR; } if (todo) { str += todo + BR; } if (journal) { str += journal + BR; } if (alarm) { str += alarm + BR; } str += 'END:VCALENDAR'; return str; };