<?php
/**
 * Small command parsing helpers used across admin autonomy transports.
 *
 * @package Niwa_AI
 */

defined( 'ABSPATH' ) || exit;

class Niwa_AI_Admin_Autonomy_Command_Utils {
	public function normalize_command_text( $text ) {
		$text = trim( wp_strip_all_tags( (string) $text ) );
		$text = remove_accents( $text );
		$text = strtolower( $text );
		$text = str_replace( array( '*', '_', '`', '~' ), '', $text );
		$text = preg_replace( '/[^\p{L}\p{N}\s\/\-\+]+/u', ' ', $text );
		$text = preg_replace( '/\s+/', ' ', $text );
		return trim( ltrim( (string) $text, '/' ) );
	}

	public function build_remove_keyboard() {
		return array(
			'remove_keyboard' => true,
		);
	}

	public function is_stop_command_text( $text ) {
		$text = trim( (string) $text );
		if ( '' === $text ) {
			return false;
		}

		if ( 1 === preg_match( '/^\/stop(?:@\w+)?(?:\s|$)/i', $text ) ) {
			return true;
		}

		return in_array(
			$this->normalize_command_text( $text ),
			array( 'stop', 'stani', 'zaustavi', 'prekini', 'ugasi', 'dosta' ),
			true
		);
	}
}
