export class MondaySync {
    client;
    mapper;
    constructor(client, mapper) {
        this.client = client;
        this.mapper = mapper;
    }
    async syncTicket(ticket, boardId) {
        const itemName = this.ticketToItemName(ticket);
        const mapping = this.mapper.getByTicketId(ticket.id);
        if (mapping && mapping.mondayBoardId === boardId) {
            if (mapping.mondayItemName !== itemName) {
                await this.client.updateItemName(boardId, mapping.mondayItemId, itemName);
            }
            this.mapper.createOrUpdateMapping({
                pmoTicketId: ticket.id,
                mondayBoardId: boardId,
                mondayItemId: mapping.mondayItemId,
                mondayItemName: itemName,
                mondayItemUrl: mapping.mondayItemUrl,
                syncDirection: 'outbound',
            });
            return {
                created: false,
                itemId: mapping.mondayItemId,
            };
        }
        const createdItem = await this.client.createItem(boardId, itemName);
        this.mapper.createOrUpdateMapping({
            pmoTicketId: ticket.id,
            mondayBoardId: boardId,
            mondayItemId: createdItem.id,
            mondayItemName: createdItem.name,
            mondayItemUrl: createdItem.url,
            syncDirection: 'outbound',
        });
        return {
            created: true,
            itemId: createdItem.id,
        };
    }
    ticketToItemName(ticket) {
        return `${ticket.id}: ${ticket.title}`;
    }
}
//# sourceMappingURL=sync.js.map