<?php

namespace App\Events\Matrix;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class Factory {
	private const LOGKEY = 'EMf';

	/**
	 * @var array event type to event class mapping
	 */
	public const map = [
		'm.room.message' => Message::class,
	];

	/**
	 * Returns new event instance
	 *
	 * @param string $type
	 * @param array  $request
	 * @return Base
	 */
	public static function create(string $type,array $request): Base
	{
		$class = Arr::get(self::map,$type,Unknown::class);
		Log::debug(sprintf('%s:- Working out Event Class for [%s] as [%s]',static::LOGKEY,$type,$class));

		if (App::environment() == 'local')
			file_put_contents('/tmp/event.'.$type,print_r($request,TRUE));

		return new $class($request);
	}

	public static function make(array $request): Base
	{
		return self::create(Arr::get($request,'type','unknown'),$request);
	}
}