<?php

namespace Planaday\Api\V1;

use Planaday\Api\ApiException\ApiErrorResponseException;
use Planaday\Api\ApiException\ClientNotInitializedException;
use Planaday\Api\LoggingApi;
use Planaday\Utility\Debug\Log\LogEntry;

class LoggingApiV1 implements LoggingApi{

	public function __construct(
		protected ClientV1 $client
	) {}

	public function sendErrorLog( LogEntry $log ): void {
		try {
			$this->client->callEndpoint(
				'log',
				[],
				'POST',
				$log->getAsArray()
			);
		} catch ( ApiErrorResponseException|ClientNotInitializedException ) {
			return;
		}
	}

	public function handleLog( LogEntry $entry ): void {
		if ( $entry->getType() === "error" )
			$this->sendErrorLog($entry);
	}
}