import { defineComponent, h, nextTick, onBeforeUnmount, ref, Teleport, watch } from "vue";
import { iconNode, trapFocus } from "../utils.js";

let modalId = 0;

export default defineComponent({
	name: "GnModal",
	props: {
		open: { type: Boolean, default: false },
		title: { type: String, default: "" },
		closeOnBackdrop: { type: Boolean, default: true }
	},
	emits: ["update:open", "close"],
	setup(props, { emit, slots }) {
		const titleId = `gn-modal-title-${++modalId}`;
		const dialogRef = ref(null);
		let previousFocus = null;
		const close = () => {
			emit("update:open", false);
			emit("close");
		};
		const onKeydown = event => {
			if(event.key === "Escape") {
				event.preventDefault();
				close();
			} else {
				trapFocus(event, dialogRef.value);
			}
		};
		const focusDialog = () => {
			nextTick(() => {
				dialogRef.value?.focus();
			});
		};

		watch(() => props.open, open => {
			if(open) {
				previousFocus = document.activeElement;
				document.addEventListener("keydown", onKeydown);
				focusDialog();
			} else {
				document.removeEventListener("keydown", onKeydown);
				previousFocus?.focus?.();
				previousFocus = null;
			}
		}, { flush: "post" });

		onBeforeUnmount(() => {
			document.removeEventListener("keydown", onKeydown);
		});

		return () => props.open ? h(Teleport, { to: "body" }, [
			h("div", { class: "modal a-show", "aria-hidden": "false" }, [
				h("div", {
					class: "modal-backdrop",
					onClick: () => props.closeOnBackdrop && close()
				}),
				h("div", {
					ref: dialogRef,
					class: "modal-dialog",
					role: "dialog",
					"aria-modal": "true",
					"aria-labelledby": titleId,
					tabindex: "-1"
				}, [
					h("header", { class: "modal-header" }, [
						h("h4", { class: "modal-title", id: titleId }, slots.title?.() || props.title),
						h("button", {
							class: "btn-icon modal-close",
							type: "button",
							"aria-label": "Close",
							onClick: close
						}, [iconNode("ph-x")])
					]),
					h("div", { class: "modal-panel" }, [
						h("div", { class: "modal-body" }, slots.default?.()),
						(slots.footer || slots.actions) && h("footer", { class: "modal-footer" }, [
							slots.footer?.(),
							slots.actions && h("div", { class: "actions" }, slots.actions({ close }))
						])
					])
				])
			])
		]) : null;
	}
});
