import { getPropertyMap } from "./_internal/getPropertyMap";
import { EventPropertyMapper } from "./_internal/PropertyMapper";
import { buildDefaultEventEntity, buildDefaultEventRecord } from "./defaults";
import { createEvent as createEventApi, deleteEvent as deleteEventApi, updateEvent as updateEventApi, } from "./api/events";
import { buildEventAssociations } from "./_internal/buildEventAssociations";
import { createRegistration, deleteRegistration } from "./api/registrations";
/**
 * @private
 * Create a new Hub Event item
 *
 * Minimal properties are name and orgUrlKey
 *
 * @param partialEvent a partial event
 * @param requestOptions user request options
 * @returns promise that resolves an IHubEvent
 */
export async function createHubEvent(partialEvent, requestOptions) {
    const event = Object.assign(Object.assign({}, buildDefaultEventEntity()), partialEvent);
    // single-day events are created from new-menu, i.e. no endDate field provided
    // so set endDate to startDate
    event.endDate = event.startDate;
    // TODO: how to handle events being discussable vs non-discussable
    const mapper = new EventPropertyMapper(getPropertyMap());
    let model = mapper.entityToStore(event, buildDefaultEventRecord());
    const associations = await buildEventAssociations(partialEvent.referencedContentIdsByType, partialEvent.referencedContentIds, requestOptions);
    const data = {
        access: model.access,
        allDay: model.allDay,
        allowRegistration: model.allowRegistration,
        associations,
        attendanceType: model.attendanceType,
        capabilities: model.capabilities,
        categories: model.categories,
        description: model.description,
        editGroups: model.editGroups,
        endDate: model.endDate,
        endTime: model.endTime,
        inPersonCapacity: model.inPersonCapacity,
        notifyAttendees: model.notifyAttendees,
        onlineMeeting: model.onlineMeeting,
        readGroups: model.readGroups,
        startDate: model.startDate,
        startTime: model.startTime,
        summary: model.summary,
        tags: model.tags,
        timeZone: model.timeZone,
        title: model.title,
        location: model.location,
    };
    model = await createEventApi(Object.assign({ data }, requestOptions));
    return mapper.storeToEntity(model, {});
}
/**
 * @private
 * Update a Hub Event
 * @param event the event to update
 * @param requestOptions user request options
 * @returns promise that resolves a IHubEvent
 */
export async function updateHubEvent(partialEvent, requestOptions) {
    var _a, _b;
    const eventUpdates = Object.assign(Object.assign({}, buildDefaultEventEntity()), partialEvent);
    // TODO: how to handle events being discussable vs non-discussable
    const mapper = new EventPropertyMapper(getPropertyMap());
    let model = mapper.entityToStore(eventUpdates, buildDefaultEventRecord());
    const associations = await buildEventAssociations(partialEvent.referencedContentIdsByType, partialEvent.referencedContentIds, requestOptions);
    const data = {
        access: model.access,
        allDay: model.allDay,
        allowRegistration: model.allowRegistration,
        associations,
        attendanceType: model.attendanceType,
        capabilities: model.capabilities,
        categories: model.categories,
        description: ((_a = model.description) === null || _a === void 0 ? void 0 : _a.trim()) || null,
        editGroups: model.editGroups,
        endDate: model.endDate,
        endTime: model.endTime,
        inPersonCapacity: model.inPersonCapacity,
        notifyAttendees: model.notifyAttendees,
        onlineMeeting: model.onlineMeeting,
        readGroups: model.readGroups,
        startDate: model.startDate,
        startTime: model.startTime,
        status: model.status,
        summary: ((_b = model.summary) === null || _b === void 0 ? void 0 : _b.trim()) || null,
        tags: model.tags,
        thumbnailUrl: model.thumbnailUrl || null,
        timeZone: model.timeZone,
        title: model.title,
        location: model.location,
    };
    model = await updateEventApi(Object.assign({ eventId: model.id, data }, requestOptions));
    return mapper.storeToEntity(model, {});
}
export async function deleteHubEvent(id, requestOptions) {
    // TODO: update `status` of event to `"removed"` when requestOptions.params.parementDelete is `true`
    // instead of permanently deleting the event when we officially support recycle bin behavior
    await deleteEventApi(Object.assign({ eventId: id }, requestOptions));
}
/**
 * @private
 * Create an Event registration
 * @param data
 * @param requestOptions
 * @returns Promise<void>
 */
export function createHubEventRegistration(data, requestOptions) {
    return createRegistration(Object.assign({ data }, requestOptions));
}
/**
 * @private
 * Remove an Event Attendee
 * @param id event attendee id
 * @param requestOptions
 * @returns Promise<void>
 */
export async function deleteHubEventRegistration(id, requestOptions) {
    await deleteRegistration(Object.assign({ registrationId: id }, requestOptions));
}
//# sourceMappingURL=edit.js.map