import {useConfirm} from "primevue/useconfirm";

class Confirm {
	_ref = null
	_header = null
	_message = ""
	_target = ""
	_icon = "pi pi-exclamation-circle"
	_onAccept = undefined
	_onReject = undefined
	_onClose = undefined
	_rejectProps = {
		label: "Cancel",
		severity: "secondary",
		style: {"min-width": "5em"}
	}
	_acceptProps = {
		label: "Confirm",
		style: {"min-width": "5em"}
	}
	_acceptLabel = "Confirm"
	_rejectLabel = "Reject"

	constructor(ref, message) {
		this._ref = ref
		this._message = message
	}

	header(header) {
		this._header = header
		return this
	}

	target(target) {
		this._target = target
		return this
	}

	event(event) {
		this._target = event.currentTarget
		return this
	}

	icon(icon) {
		this._icon = icon
		return this
	}

	acceptProps(acceptProps) {
		this._acceptProps = acceptProps
		return this
	}

	rejectProps(rejectProps) {
		this._rejectProps = rejectProps
		return this
	}

	acceptLabel(label) {
		this._acceptLabel = label
		if (this._acceptProps != null && typeof this._acceptProps === "object" && typeof this._acceptProps.label === "string") {
			this._acceptProps.label = this._acceptLabel
		}
		return this
	}

	rejectLabel(label) {
		this._rejectLabel = label
		if (this._rejectProps != null && typeof this._rejectProps === "object" && typeof this._rejectProps.label === "string") {
			this._rejectProps.label = this._rejectLabel
		}
		return this
	}

	onAccept(f) {
		this._onAccept = f
		return this
	}

	onReject(f) {
		this._onReject = f
		return this
	}

	onClose(f) {
		this._onClose = f
		return this
	}

	show() {
		this._ref.require({
			message: this._message,
			target: this._target,
			icon: this._icon,
			accept: this._onAccept,
			reject: this._onReject,
			onHide: this._onClose,
			rejectProps: this._rejectProps,
			acceptProps: this._acceptProps,
			acceptLabel: this._acceptLabel,
			rejectLabel: this._rejectLabel,
			header: this._header
		})
	}
}

export const useConfirmService = () => {
	const ref = useConfirm()

	const newConfirm = (message) => {
		return new Confirm(ref, message)
	}

	return {newConfirm}
}