"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoteReducer = exports.GetNoteSelector = exports.GetAllNotesSelector = exports.NoteReady = exports.NoteDelete = exports.NoteEdit = exports.NoteAdd = exports.NOTE_READY = exports.NOTE_DELETE = exports.NOTE_EDIT = exports.NOTE_ADD = void 0;
const tslib_1 = require("tslib");
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../Utilities/Helpers/AdaptableHelper"));
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
/**
 * @ReduxAction A Note has been added
 */
exports.NOTE_ADD = 'NOTE_ADD';
/**
 * @ReduxAction A Note has been edited
 */
exports.NOTE_EDIT = 'NOTE_EDIT';
/**
 * @ReduxAction A Note has been deleted
 */
exports.NOTE_DELETE = 'NOTE_DELETE';
/**
 * @ReduxAction Note Module is ready
 */
exports.NOTE_READY = 'NOTE_READY';
const NoteAdd = (note) => ({
    type: exports.NOTE_ADD,
    adaptableNote: note,
});
exports.NoteAdd = NoteAdd;
const NoteEdit = (note) => ({
    type: exports.NOTE_EDIT,
    adaptableNote: note,
});
exports.NoteEdit = NoteEdit;
const NoteDelete = (note) => ({
    type: exports.NOTE_DELETE,
    adaptableNote: note,
});
exports.NoteDelete = NoteDelete;
const NoteReady = (note) => ({
    type: exports.NOTE_READY,
    noteState: note,
});
exports.NoteReady = NoteReady;
const GetAllNotesSelector = (state) => state.Notes;
exports.GetAllNotesSelector = GetAllNotesSelector;
const GetNoteSelector = (state, address) => {
    if (!address) {
        return null;
    }
    return (state?.Notes ?? []).find((note) => {
        if (note.PrimaryKeyValue === address.PrimaryKeyValue && note.ColumnId === address.ColumnId) {
            // happy check
            return true;
        }
        // Primary keys retreived from the grid dom are always strings, so we must also consider them strings
        if ((typeof address.PrimaryKeyValue === 'number' && typeof note.PrimaryKeyValue === 'string') ||
            (typeof address.PrimaryKeyValue === 'string' && typeof note.PrimaryKeyValue === 'number')) {
            return (note.PrimaryKeyValue.toString() === address.PrimaryKeyValue.toString() &&
                note.ColumnId === address.ColumnId);
        }
        return false;
    });
};
exports.GetNoteSelector = GetNoteSelector;
const initialState = {
    Notes: GeneralConstants_1.EMPTY_ARRAY,
};
const NoteReducer = (state = initialState, action) => {
    let adaptableNotes;
    switch (action.type) {
        case exports.NOTE_ADD: {
            const note = action.adaptableNote;
            AdaptableHelper_1.default.addAdaptableObjectPrimitives(note);
            adaptableNotes = [...state.Notes, note];
            return { ...state, Notes: adaptableNotes };
        }
        case exports.NOTE_EDIT: {
            const note = action.adaptableNote;
            return {
                ...state,
                Notes: state.Notes.map((abObject) => (abObject.Uuid === note.Uuid ? note : abObject)),
            };
        }
        case exports.NOTE_DELETE: {
            const note = action.adaptableNote;
            return {
                ...state,
                Notes: state.Notes.filter((abObject) => abObject.Uuid !== note.Uuid),
            };
        }
        default:
            return state;
    }
};
exports.NoteReducer = NoteReducer;
