<!--
Order composer tool. Resolves the per-game `OrderDraftStore` from
context (set by `routes/games/[id]/+layout.svelte`) and renders the
draft as a vertical, top-to-bottom command list. Empty state shows
the i18n empty-state copy; non-empty state shows an ordered list of
rows, each with a stable `data-testid` plus a per-row delete button.

Phase 12 has no UI for adding commands — Phase 14 lands the first
end-to-end command (`planetRename`) and the inspector affordance
that pushes it into the draft. Tests exercise the skeleton through
`__galaxyDebug.seedOrderDraft` (Playwright) and via direct store
construction (Vitest).
-->
<script lang="ts">
	import { getContext } from "svelte";
	import { i18n } from "$lib/i18n/index.svelte";
	import {
		ORDER_DRAFT_CONTEXT_KEY,
		OrderDraftStore,
	} from "../../sync/order-draft.svelte";

	const draft = getContext<OrderDraftStore | undefined>(
		ORDER_DRAFT_CONTEXT_KEY,
	);

	function describe(cmd: { kind: string; label?: string }): string {
		if (cmd.kind === "placeholder") return cmd.label ?? cmd.kind;
		return cmd.kind;
	}
</script>

<section class="tool" data-testid="sidebar-tool-order">
	<h3>{i18n.t("game.sidebar.tab.order")}</h3>
	{#if draft === undefined || draft.commands.length === 0}
		<p class="empty" data-testid="order-empty">
			{i18n.t("game.sidebar.empty.order")}
		</p>
	{:else}
		<ol class="commands" data-testid="order-list">
			{#each draft.commands as cmd, index (cmd.id)}
				<li class="command" data-testid="order-command-{index}">
					<span class="index" aria-hidden="true">{index + 1}.</span>
					<span class="label" data-testid="order-command-label-{index}">
						{describe(cmd)}
					</span>
					<button
						type="button"
						class="delete"
						data-testid="order-command-delete-{index}"
						onclick={() => draft?.remove(cmd.id)}
					>
						{i18n.t("game.sidebar.order.command_delete")}
					</button>
				</li>
			{/each}
		</ol>
	{/if}
</section>

<style>
	.tool {
		padding: 1rem;
		font-family: system-ui, sans-serif;
	}
	.tool h3 {
		margin: 0 0 0.5rem;
		font-size: 1rem;
	}
	.empty {
		margin: 0;
		color: #888;
	}
	.commands {
		list-style: none;
		margin: 0;
		padding: 0;
		display: flex;
		flex-direction: column;
		gap: 0.25rem;
	}
	.command {
		display: flex;
		align-items: center;
		gap: 0.5rem;
		padding: 0.4rem 0.5rem;
		background: #14182a;
		border: 1px solid #20253a;
		border-radius: 4px;
	}
	.index {
		min-width: 1.5rem;
		color: #aab;
		font-variant-numeric: tabular-nums;
	}
	.label {
		flex: 1;
		min-width: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.delete {
		font: inherit;
		font-size: 0.85rem;
		padding: 0.2rem 0.55rem;
		background: transparent;
		color: #aab;
		border: 1px solid #2a3150;
		border-radius: 3px;
		cursor: pointer;
	}
	.delete:hover {
		color: #e8eaf6;
		border-color: #6d8cff;
	}
</style>
