<?php
	include_once("source/logic/server/data.php");
	include_once("source/logic/server/table.php");
	include_once("source/logic/server/misc.php");
	include_once("source/logic/server/sql.php");
 ?>
<section class="subsection" id="correlatives">
	<header>Correlatives</header>
	<?php proposal(); ?>
<?php
	$mode = 0;
	switch ($mode)
	{
		case 0:
		{
			$table = new class_table
			(
				[
					new class_column
					(
						"Domain",
						function ($row) {return $row["domain"];}
					),
					new class_column
					(
						"Interrogative / Relative",
						_foo("interrogative"),
						_bar("word")
					),
					new class_column
					(
						"Demonstrative far",
						_foo("demonstrative_far"),
						_bar("word")
					),
					new class_column
					(
						"Demonstrative near",
						_foo("demonstrative_near"),
						_bar("word")
					),
					new class_column
					(
						"Universal",
						_foo("universal"),
						_bar("word")
					),
					new class_column
					(
						"Undefined",
						_foo("undefined"),
						_bar("word")
					),
					new class_column
					(
						"Negative",
						_foo("negative"),
						_bar("word")
					),
				],
				read_json("source/data/correlatives.json")
			);
			$table->generate();
			break;
		}
		case 1:
		{
			global $configuration;
			$database = read_json("source/data/x.json");
			$data = $database["words"];
			// $data = sql_cross($database["words"], sql_rename($database["words"]));
			$data = sql_select($data, function ($row) {return ($row["language"] == "fs");});
			$table = new class_table
			(
				[
					new class_column("Domain (id)", function ($row) {return $row["domain_id"];}),
					new class_column("Type (id)", function ($row) {return $row["type_id"];}),
					new class_column
					(
						"Folksprak",
						function ($row) {return $row["words"];},
						function ($value) {global $configuration; return _baz($value, "word", "fs");}
					),
					/*
					new class_column
					(
						$configuration["languagemap"][$configuration["target"]],
						function ($row) {global $configuration; return $row["words"][$configuration["target"]];},
						function ($value) {global $configuration; return _baz($value, "word", $configuration["target"]);}
					),
					 */
				],
				$data
			);
			$table->generate();
			break;
		}
		case 2:
		{
			global $configuration;
			$database = read_json("source/data/x.json");
			$data = $database["words"];
			
			// get domain names
			{
				$data = sql_cross($data, $database["domains"]);
				$data = sql_rename($data, "name", "domain_name");
				$data = sql_select($data, function ($row) {return ($row["id"] == $row["domain_id"]);});
				$data = sql_delete($data, ["id"]);
			}
			
			// get type names
			{
				$data = sql_cross($data, $database["types"]);
				$data = sql_rename($data, "name", "type_name");
				$data = sql_select($data, function ($row) {return ($row["id"] == $row["type_id"]);});
				$data = sql_delete($data, ["id"]);
			}
			
			// transform
			{
				$data = sql_select($data, function ($row) {global $configuration; return (($row["language"] == "fs") or ($row["language"] == $configuration["target"]));});
				// $data = sql_group($data, "language");
				$data = sql_add($data, ["type_name/words"], ["type_name/words" => function ($row) {return ["key" => $row["type_id"], "value" => [$row["language"] => $row["words"]]];}]);
				$data = sql_project($data, ["domain_name", "type_name/words"]);
				$data = sql_group
				(
					$data,
					"domain_name",
					[
						"type_name/words" => function ($args)
						{
							$output = [];
							foreach ($args as $arg)
							{
								if (! array_key_exists($arg["key"], $output)) $output[$arg["key"]] = [];
								foreach ($arg["value"] as $key => $value)
								{
									$output[$arg["key"]][$key] = $value;
								}
							}
							return $output;
						},
					]
				);
			}
			
			$table = new class_table
			(
				array_merge
				(
					[
						new class_column("Domain", function ($row) {return $row["domain_name"];}),
						// new class_column("type_name/words", function ($row) {return $row["type_name/words"];}, function ($value) {print_r($value);}),
					],
					array_map
					(
						function ($type)
						{
							return (
								new class_column
								(
									("Type: " . $type["name"]),
									function ($row) use (&$type)
									{
										global $configuration;
										return ["original" => $row["type_name/words"][$type["id"]]["fs"], "translated" => $row["type_name/words"][$type["id"]][$configuration["target"]]];
									},
									_bar("word")
								)
							);
						},
						array_filter($database["types"], function ($type) {return ($type["id"] != "relative");})
					)
				),
				$data
			);
			$table->generate();
			break;
		}
		default:
		{
			break;
		}
	}
 ?>
<div class="note_information">Some of these words are very unlikely to ever get used (e.g. <span class="word lang_fs">ing slag</span>). Nevertheless they exist and are listed for completeness.</div>
</section>