export interface ModalConfig {
    title?: string;
    content: HTMLElement | string;
    onClose?: () => void;
    backdropClick?: boolean;
}

export class ModalFactory {
    public static create(config: ModalConfig): HTMLElement {
        const modal = document.createElement('div');
        modal.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 9999;
        `;

        const modalContent = document.createElement('div');
        modalContent.style.cssText = `
            background: white;
            border-radius: 8px;
            padding: 24px;
            max-width: 90%;
            max-height: 90%;
            overflow: auto;
        `;

        if (config.title) {
            const title = document.createElement('h2');
            title.textContent = config.title;
            modalContent.appendChild(title);
        }

        if (typeof config.content === 'string') {
            modalContent.innerHTML += config.content;
        } else {
            modalContent.appendChild(config.content);
        }

        modal.appendChild(modalContent);

        if (config.backdropClick !== false) {
            modal.addEventListener('click', (e) => {
                if (e.target === modal) {
                    ModalFactory.close(modal);
                    config.onClose?.();
                }
            });
        }

        return modal;
    }

    public static close(modal: HTMLElement): void {
        modal.remove();
    }
}
