import test from "node:test";
import assert from "node:assert/strict";
import {
	evaluateConditionSet,
	normalizeConditionSet,
	ConditionKind
} from "../../shared/quests/conditions.js";
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";

test("ConditionSet AND + NOT", () =>
{
	const set = normalizeConditionSet({
		all: [
			{ condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
			{ not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
		]
	});
	assert.equal(evaluateConditionSet(set, {
		countItem: (id) => (id === 1 ? 2 : 0),
		questState: () => "not-started"
	}), true);
	assert.equal(evaluateConditionSet(set, {
		countItem: (id) => (id === 1 ? 2 : 0),
		questState: () => "completed"
	}), false);
});

test("normalize quest and dialogue", () =>
{
	const quest = normalizeQuestDef({
		questId: "Fetch Ore!",
		title: "Fetch Ore",
		steps: [{
			objectives: [{ text: "Have tin", when: { all: [{ kind: "hasItem", itemId: 10, quantity: 1 }] } }]
		}]
	});
	assert.equal(quest.questId, "fetch-ore");
	assert.equal(quest.steps[0].objectives[0].when.all[0].condition.kind, "hasItem");

	const tree = normalizeDialogueTree({
		dialogueId: "Smith",
		unique: true,
		steps: [{
			stepId: "hi",
			lines: [{ speaker: "npc", text: "Hello" }],
			itemRewards: [
				{ itemId: 10, quantity: 2 },
				{ itemId: 10, quantity: 9 },
				{ itemId: 0, quantity: 1 }
			],
			exit: { kind: "end" }
		}]
	});
	assert.equal(tree.dialogueId, "smith");
	assert.equal(tree.unique, true);
	assert.equal(tree.steps[0].exit.kind, "end");
	assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);
	assert.deepEqual(tree.steps[0].questRewards, []);
});

test("dialogue reward messages bag vs bank and lists", () =>
{
	assert.equal(
		formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 1 }], "bag"),
		"Tin ore has been added to your bag."
	);
	assert.equal(
		formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 3 }], "bank"),
		"3 Tin ore have been sent to your bank."
	);
	assert.equal(
		formatDialogueRewardMessage([
			{ displayName: "Tin ore", quantity: 1 },
			{ displayName: "Copper ore", quantity: 1 }
		], "bag"),
		"Tin ore and Copper ore have been added to your bag."
	);
	assert.equal(
		formatDialogueRewardMessage([
			{ displayName: "A", quantity: 1 },
			{ displayName: "B", quantity: 1 },
			{ displayName: "C", quantity: 1 }
		], "bag"),
		"A, B and C have been added to your bag."
	);
	assert.equal(
		formatDialogueQuestRewardMessage(["Tin Delivery"]),
		"The Tin Delivery quest has been added to your journal."
	);
	assert.equal(
		formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
		"The Alpha and Beta quests have been added to your journal."
	);
});
