"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentsReducer = exports.GetCellCommentSelector = exports.CommentsDelete = exports.CommentsEdit = exports.CommentsCellDelete = exports.CommentsCellAdd = exports.CommentsAdd = exports.CommentsSet = exports.COMMENTS_LOAD = exports.COMMENTS_READY = exports.COMMENTS_DELETE = exports.COMMENTS_EDIT = exports.COMMENTS_CELL_DELETE = exports.COMMENTS_CELL_ADD = exports.COMMENTS_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 Comments has been added
 */
exports.COMMENTS_ADD = 'COMMENTS_ADD';
/**
 * @ReduxAction Cell Comments has been added
 */
exports.COMMENTS_CELL_ADD = 'COMMENTS_CELL_ADD';
/**
 * @ReduxAction Cell comments have been removed
 */
exports.COMMENTS_CELL_DELETE = 'COMMENTS_CELL_DELETE';
/**
 * @ReduxAction A Comments has been edited
 */
exports.COMMENTS_EDIT = 'COMMENTS_EDIT';
/**
 * @ReduxAction A Comments has been deleted
 */
exports.COMMENTS_DELETE = 'COMMENTS_DELETE';
/**
 * @ReduxAction Comments Module is ready
 */
exports.COMMENTS_READY = 'COMMENTS_READY';
exports.COMMENTS_LOAD = 'COMMENTS_LOAD';
const CommentsSet = ({ commentThread, }) => ({
    type: exports.COMMENTS_LOAD,
    commentThread,
});
exports.CommentsSet = CommentsSet;
const CommentsAdd = ({ comment, cellAddress, }) => ({
    type: exports.COMMENTS_ADD,
    comment,
    cellAddress,
});
exports.CommentsAdd = CommentsAdd;
const CommentsCellAdd = ({ commentThread, }) => ({
    type: exports.COMMENTS_CELL_ADD,
    commentThread,
});
exports.CommentsCellAdd = CommentsCellAdd;
const CommentsCellDelete = ({ cellAddress: cellAddress, }) => ({
    type: exports.COMMENTS_CELL_DELETE,
    cellAddress: cellAddress,
});
exports.CommentsCellDelete = CommentsCellDelete;
const CommentsEdit = ({ comment, cellAddress, }) => ({
    type: exports.COMMENTS_EDIT,
    comment,
    cellAddress,
});
exports.CommentsEdit = CommentsEdit;
const CommentsDelete = ({ commentUuid, cellAddress, }) => ({
    type: exports.COMMENTS_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.COMMENTS_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.COMMENTS_CELL_ADD: {
            return {
                ...state,
                CommentThreads: [
                    ...(state.CommentThreads ?? []),
                    action.commentThread,
                ],
            };
        }
        case exports.COMMENTS_CELL_DELETE: {
            const deleteAction = action;
            return {
                ...state,
                CommentThreads: (state.CommentThreads ?? []).filter((cellComments) => !AnnotationsService_1.AnnotationsService.isSameAddress(cellComments, deleteAction.cellAddress)),
            };
        }
        case exports.COMMENTS_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.COMMENTS_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_LOAD: {
            return {
                ...state,
                CommentThreads: action.commentThread,
            };
        }
        default:
            return state;
    }
};
exports.CommentsReducer = CommentsReducer;
