<?php

namespace ZenoChatbot\Framework\Foundation;

class DetectRequestType
{
    public static function detect(Application $app)
    {
        if (defined('REST_REQUEST') && \REST_REQUEST) {
            $app->setRequestType('rest');
        }

        if (defined('DOING_AJAX') && \DOING_AJAX) {
            $app->setRequestType('ajax');
        }

        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
            $app->setRequestType('ajax');
        }

        if (isset($_POST['action']) && strpos($_POST['action'], 'zenochatbot_') === 0) {
            $app->setRequestType('ajax');
        }

        if (strpos($_SERVER['REQUEST_URI'] ?? '', '/wp-json/') !== false) {
            $app->setRequestType('rest');
        }
    }
}
