<?php
namespace EasyChat\View;

class HtmxFragmentRenderer {

    private const MOTION_BASE_CLASS = 'htmx-fragment';
    private const MOTION_KIND_PREFIX = 'htmx-fragment-';
    private const QUICK_REPLY_ROW_SWAP = 'outerMorph transition:true';
    private const QUICK_REPLY_LIST_SWAP = 'innerMorph transition:true';
    private const QUICK_REPLY_APPEND_SWAP = 'beforeend transition:true';
    private const OPERATOR_ROW_SWAP = 'outerHTML transition:true';
    private const OPERATOR_LIST_SWAP = 'innerHTML transition:true';
    private const SETTINGS_PANEL_SWAP = 'outerHTML transition:true';
    private const INTEGRATION_DIAGNOSTICS_SWAP = 'outerHTML transition:true';
    private const KNOWLEDGE_LIST_SWAP = 'outerHTML transition:true';
    private const NOTIFICATION_HEALTH_PULSE_SWAP = 'outerHTML transition:true';

    public static function motionClass($kind = '') {
        $kind = preg_replace('/[^a-z0-9_-]/i', '', (string)$kind);
        return trim(self::MOTION_BASE_CLASS . ($kind !== '' ? ' ' . self::MOTION_KIND_PREFIX . $kind : ''));
    }

    public static function quickReplyRowSwap() {
        return self::QUICK_REPLY_ROW_SWAP;
    }

    public static function quickReplyListSwap() {
        return self::QUICK_REPLY_LIST_SWAP;
    }

    public static function quickReplyAppendSwap() {
        return self::QUICK_REPLY_APPEND_SWAP;
    }

    public static function operatorRowSwap() {
        return self::OPERATOR_ROW_SWAP;
    }

    public static function operatorListSwap() {
        return self::OPERATOR_LIST_SWAP;
    }

    public static function settingsPanelSwap() {
        return self::SETTINGS_PANEL_SWAP;
    }

    public static function integrationDiagnosticsSwap() {
        return self::INTEGRATION_DIAGNOSTICS_SWAP;
    }

    public static function knowledgeListSwap() {
        return self::KNOWLEDGE_LIST_SWAP;
    }

    public static function notificationHealthPulseSwap() {
        return self::NOTIFICATION_HEALTH_PULSE_SWAP;
    }

    public function sendHtml($html, $status = 200, array $headers = []) {
        http_response_code((int)$status);
        header('Content-Type: text/html; charset=utf-8');
        foreach ($headers as $name => $value) {
            if ($value === null || $value === '') {
                continue;
            }
            header($this->safeHeaderName($name) . ': ' . $this->safeHeaderValue($value));
        }
        echo (string)$html;
        exit;
    }

    public function sendFragment($html, array $trigger = []) {
        $headers = [];
        if ($trigger) {
             
             
             
            $encoded = json_encode($trigger, JSON_UNESCAPED_SLASHES);
            $headers['HX-Trigger'] = is_string($encoded) ? $encoded : '{}';
        }
        $this->sendHtml($html, 200, $headers);
    }

    public function sendAlert($target, $html, $status = 400, $swap = 'outerHTML') {
        $this->sendHtml($html, (int)$status, [
            'HX-Retarget' => (string)$target,
            'HX-Reswap' => (string)$swap,
        ]);
    }

    private function safeHeaderName($name) {
        $name = (string)$name;
        if (!preg_match('/^[A-Za-z0-9-]+$/', $name)) {
            throw new \InvalidArgumentException('Invalid htmx header name');
        }
        return $name;
    }

    private function safeHeaderValue($value) {
        return str_replace(["\r", "\n"], '', (string)$value);
    }

}
