/**
 * TableEventManager handles dispatching of custom events for table operations.
 * Provides type-safe event triggering methods and centralized event management.
 */
export class TableEventManager {
    element;
    constructor(element = null) {
        this.element = element ?? document;
    }
    /**
     * Updates the element reference for event dispatching
     */
    setElement(element) {
        this.element = element ?? document;
    }
    printWarning(message) {
        if (process.env.NODE_ENV === 'development') {
            // This is a warning that is only shown in development mode for better DX
            // eslint-disable-next-line no-console
            console.warn(message);
        }
    }
    /**
     * Generic method to trigger any table event
     */
    triggerEvent(eventType, payload) {
        if (!this.element) {
            this.printWarning(`TableEventManager: Cannot dispatch event '${eventType}' - no element reference`);
            return;
        }
        const event = new CustomEvent(eventType, {
            detail: payload,
            bubbles: true,
            cancelable: false,
        });
        this.element.dispatchEvent(event);
    }
    /**
     * Triggers when a cell enters edit mode
     */
    triggerEditingStart(payload) {
        this.triggerEvent('table:editing:start', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when a cell edit is successfully saved
     */
    triggerEditingSave(payload) {
        this.triggerEvent('table:editing:save', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when exiting edit mode (regardless of save/cancel)
     */
    triggerEditingExit(payload) {
        this.triggerEvent('table:editing:exit', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when validation fails
     */
    triggerValidationError(payload) {
        this.triggerEvent('table:editing:validationError', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when validation succeeds
     */
    triggerValidationSuccess(payload) {
        this.triggerEvent('table:editing:validationSuccess', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when validation error state changes
     */
    triggerValidationErrorChange(payload) {
        this.triggerEvent('table:editing:validationErrorChange', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when a delete operation starts
     */
    triggerDeleteStart(payload) {
        this.triggerEvent('table:delete:start', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when user confirms the deletion
     */
    triggerDeleteConfirm(payload) {
        this.triggerEvent('table:delete:confirm', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when deletion is successfully completed
     */
    triggerDeleteSuccess(payload) {
        this.triggerEvent('table:delete:success', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when deletion is cancelled
     */
    triggerDeleteCancel(payload) {
        this.triggerEvent('table:delete:cancel', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when deletion fails due to an error
     */
    triggerDeleteError(payload) {
        this.triggerEvent('table:delete:error', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when a new row insertion starts
     */
    triggerInsertStart(payload) {
        this.triggerEvent('table:insert:start', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when a new row insertion is successfully completed
     */
    triggerInsertSuccess(payload) {
        this.triggerEvent('table:insert:success', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when a new row insertion is cancelled
     */
    triggerInsertCancel(payload) {
        this.triggerEvent('table:insert:cancel', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when column sort direction changes
     */
    triggerSortChange(payload) {
        this.triggerEvent('table:sort:change', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Triggers when column sorting is cleared
     */
    triggerSortClear(payload) {
        this.triggerEvent('table:sort:clear', {
            ...payload,
            timestamp: new Date(),
        });
    }
    /**
     * Add an event listener for a specific table event
     */
    addEventListener(eventType, listener, options) {
        if (!this.element) {
            this.printWarning(`TableEventManager: Cannot add listener for '${eventType}' - no element reference`);
            return;
        }
        this.element.addEventListener(eventType, listener, options);
    }
    /**
     * Remove an event listener for a specific table event
     */
    removeEventListener(eventType, listener, options) {
        if (!this.element) {
            this.printWarning(`TableEventManager: Cannot remove listener for '${eventType}' - no element reference`);
            return;
        }
        this.element.removeEventListener(eventType, listener, options);
    }
}
//# sourceMappingURL=table-event-manager.js.map