<?php

// @codeCoverageIgnoreStart
require_once __DIR__ . '/../../../maintenance/Maintenance.php';
// @codeCoverageIgnoreEnd
use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
use MediaWiki\Maintenance\Maintenance;

class UpdateTexutil extends Maintenance {
	private const GROUP = 'callback';

	public function execute() {
		$jsonFilePath = './src/WikiTexVC/texutil.json';

		$jsonContent = json_decode( file_get_contents( $jsonFilePath ), true );

		if ( $jsonContent === null ) {
			$this->fatalError( "Failed to decode texutil.json. Please check the file format.\n" );
		}
		$tu = TexUtil::getInstance();
		foreach ( $tu->getBaseElements()[self::GROUP] as $key => $value ) {
			if ( $value[0] !== 'underOver' ) {
				continue;
			}
			$jsonContent["$key"][self::GROUP][1] = mb_chr( hexdec( $value[1] ), 'UTF-8' );

			// Sort the entry alphabetically
			ksort( $jsonContent["$key"] );
		}
		// Sort the entire file
		ksort( $jsonContent );
		$jsonString = json_encode( $jsonContent, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );

		$jsonStringWithTabs = preg_replace_callback( '/^( +)/m', static function ( $matches ) {
			// Convert spaces to tabs (assuming 4 spaces per tab level)
			return str_repeat( "\t", strlen( $matches[1] ) / 4 );
		}, $jsonString ) . "\n";
		// prevent eslint error  Unnecessary escape character: \/  no-useless-escape
		$jsonStringWithTabs = str_replace( '\/', '/', $jsonStringWithTabs );

		file_put_contents( $jsonFilePath, $jsonStringWithTabs );

		$this->output( "texutil.json successfully updated.\n" );
	}
}
// @codeCoverageIgnoreStart
$maintClass = UpdateTexutil::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd
