import { InkRunner, Utility, Processor } from "../inkrunner.js";

// puts a nice continue marker after each line of text and removes it when choices appear
// is last in the queue, so will be impacted by #delays

addEventListener(
	"StoryActive",
	(event) => {
		event.detail.inkrunner.AddProcessor(continueMarker);
	},
	{ once: true }
);

addEventListener(
	"StoryLoaded",
	(event) => {
		// if the save/load processor is loaded, add the save/load callbacks
		InkRunner.instance.AddSaveLoadCallback?.("continue", { save: saveContMarker, load: marker });
	},
	{ once: true }
);

const saveContMarker = () => {
	return InkRunner.instance.canContinue;
};

const marker = (display) => {
	if (InkRunner.instance.continueBlockStatus) return;
	document.body.querySelectorAll(`[data-ir-role="continue"]`).forEach((e) => e.remove());
	if (display) {
		InkRunner.instance.currentContainer.append(Utility.CreateElement("div", { dataset: { irRole: "continue" } }));
		addEventListener("StoryContinuing", () => marker(false), { once: true });
	}
};

const continueMarker = new Processor({
	name: "Continue marker",
	author: "isyourguy",
	description: "Adds continue marker after text (but not choices)",
	type: Processor.Type.AllElements,
	stage: InkRunner.ProcessingStage.Final,
	priority: Infinity,
	callback: async (params, inkrunner) => {		
		addEventListener("ContinueComplete", () => marker(inkrunner.canContinue), { once: true });
	},
});
