"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentsReducer = exports.GetCellCommentSelector = exports.CommentsDelete = exports.CommentEdit = exports.CommentThreadDelete = exports.CommentThreadAdd = exports.CommentAdd = exports.CommentThreadsLoad = exports.COMMENTS_THREADS_LOAD = exports.COMMENTS_READY = exports.COMMENT_DELETE = exports.COMMENT_EDIT = exports.COMMENT_THREAD_DELETE = exports.COMMENT_THREAD_ADD = exports.COMMENT_ADD = void 0;
const tslib_1 = require("tslib");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../Utilities/ObjectFactory"));
const AnnotationsService_1 = require("../../Utilities/Services/AnnotationsService");
/**
 * @ReduxAction A Comment has been added
 */
exports.COMMENT_ADD = 'COMMENT_ADD';
/**
 * @ReduxAction A Comment Thread has been added
 */
exports.COMMENT_THREAD_ADD = 'COMMENT_THREAD_ADD';
/**
 * @ReduxAction A Comment Thread has been removed
 */
exports.COMMENT_THREAD_DELETE = 'COMMENT_THREAD_DELETE';
/**
 * @ReduxAction A Comment has been edited
 */
exports.COMMENT_EDIT = 'COMMENT_EDIT';
/**
 * @ReduxAction A Comment has been deleted
 */
exports.COMMENT_DELETE = 'COMMENT_DELETE';
/**
 * @ReduxAction Comments Module is ready
 */
exports.COMMENTS_READY = 'COMMENTS_READY';
/**
 * @ReduxAction Comments have been loaded
 */
exports.COMMENTS_THREADS_LOAD = 'COMMENTS_THREADS_LOAD';
const CommentThreadsLoad = ({ commentThreads, }) => ({
    type: exports.COMMENTS_THREADS_LOAD,
    commentThreads,
});
exports.CommentThreadsLoad = CommentThreadsLoad;
const CommentAdd = ({ comment, cellAddress, }) => ({
    type: exports.COMMENT_ADD,
    comment,
    cellAddress,
});
exports.CommentAdd = CommentAdd;
const CommentThreadAdd = ({ commentThread, }) => ({
    type: exports.COMMENT_THREAD_ADD,
    commentThread,
});
exports.CommentThreadAdd = CommentThreadAdd;
const CommentThreadDelete = ({ cellAddress: cellAddress, }) => ({
    type: exports.COMMENT_THREAD_DELETE,
    cellAddress: cellAddress,
});
exports.CommentThreadDelete = CommentThreadDelete;
const CommentEdit = ({ comment, cellAddress, }) => ({
    type: exports.COMMENT_EDIT,
    comment,
    cellAddress,
});
exports.CommentEdit = CommentEdit;
const CommentsDelete = ({ commentUuid, cellAddress, }) => ({
    type: exports.COMMENT_DELETE,
    cellAddress,
    commentUuid,
});
exports.CommentsDelete = CommentsDelete;
const GetCellCommentSelector = (state, address) => {
    if (!address) {
        return undefined;
    }
    return (state?.CommentThreads ?? []).find((commentThread) => {
        return AnnotationsService_1.AnnotationsService.isSameAddress(commentThread, address);
    });
};
exports.GetCellCommentSelector = GetCellCommentSelector;
const initialState = {
    CommentThreads: GeneralConstants_1.EMPTY_ARRAY,
};
const CommentsReducer = (state = initialState, action) => {
    switch (action.type) {
        case exports.COMMENT_ADD: {
            const addAction = action;
            const commentThread = state.CommentThreads ?? [];
            const cellCommentIndex = commentThread.findIndex((cellComments) => {
                return AnnotationsService_1.AnnotationsService.isSameAddress(cellComments, addAction.cellAddress);
            });
            let comments = [...commentThread];
            if (cellCommentIndex === -1) {
                // new comments
                comments.push({
                    ...ObjectFactory_1.default.CreateEmptyCommentThread(addAction.cellAddress),
                    Comments: [addAction.comment],
                });
            }
            else {
                const existing = {
                    ...commentThread[cellCommentIndex],
                    Comments: [...commentThread[cellCommentIndex].Comments, addAction.comment],
                };
                comments[cellCommentIndex] = existing;
            }
            return { ...state, CommentThreads: comments };
        }
        case exports.COMMENT_THREAD_ADD: {
            return {
                ...state,
                CommentThreads: [
                    ...(state.CommentThreads ?? []),
                    action.commentThread,
                ],
            };
        }
        case exports.COMMENT_THREAD_DELETE: {
            const deleteAction = action;
            return {
                ...state,
                CommentThreads: (state.CommentThreads ?? []).filter((cellComments) => !AnnotationsService_1.AnnotationsService.isSameAddress(cellComments, deleteAction.cellAddress)),
            };
        }
        case exports.COMMENT_EDIT: {
            const editAction = action;
            let edited = false;
            const adaptableCellComments = (state.CommentThreads ?? []).map((cellComments) => {
                if (AnnotationsService_1.AnnotationsService.isSameAddress(cellComments, editAction.cellAddress)) {
                    // found the place
                    edited = true;
                    return {
                        ...cellComments,
                        Comments: cellComments.Comments.map((comment) => {
                            if (comment.Uuid === editAction.comment.Uuid) {
                                return editAction.comment;
                            }
                            return comment;
                        }),
                    };
                }
            });
            return {
                ...state,
                CommentThreads: adaptableCellComments,
            };
        }
        case exports.COMMENT_DELETE: {
            const deleteAction = action;
            const adaptableCellComments = (state.CommentThreads ?? [])
                .map((cellComments) => {
                if (AnnotationsService_1.AnnotationsService.isSameAddress(cellComments, deleteAction.cellAddress)) {
                    // found the place
                    const newComments = cellComments.Comments.filter((comment) => comment.Uuid !== deleteAction.commentUuid);
                    if (newComments.length === 0) {
                        return null;
                    }
                    const newCellComments = {
                        ...cellComments,
                        Comments: newComments,
                    };
                    return newCellComments;
                }
                return cellComments;
            })
                .filter((cellComments) => cellComments !== null);
            return {
                ...state,
                CommentThreads: adaptableCellComments,
            };
        }
        case exports.COMMENTS_THREADS_LOAD: {
            return {
                ...state,
                CommentThreads: action.commentThreads,
            };
        }
        default:
            return state;
    }
};
exports.CommentsReducer = CommentsReducer;
