pragma ComponentBehavior: Bound

import QtQuick
import Quickshell
import qs.Components
import ZShell.Config

Item {
	id: root

	required property real borderThickness
	readonly property alias content: content
	readonly property bool horizontal: position !== "left"
	// property real offsetScale: y > 0 || content.hasCurrent ? 0 : 1
	property real offsetScale: {
		if (content.hasCurrent)
			return 0;
		if (position === "top")
			return y > 0 ? 0 : 1;
		if (position === "bottom")
			return 1;
		return x > 0 ? 0 : 1;
	}
	required property string position
	required property ShellScreen screen

	anchors.bottom: position === "bottom" ? parent.bottom : undefined
	clip: true
	implicitHeight: horizontal ? content.implicitHeight * (1 - offsetScale) : content.implicitHeight
	implicitWidth: horizontal ? content.implicitWidth : content.implicitWidth * (1 - offsetScale)
	visible: width > 0 && height > 0
	x: {
		if (!horizontal)
			return 0;
		const off = content.currentCenter - borderThickness - content.nonAnimWidth / 2;
		const diff = parent.width - Math.floor(off + content.nonAnimWidth);
		if (diff < 0)
			return off + diff;
		return Math.floor(Math.max(off, 0));
	}
	y: {
		if (horizontal)
			return 0;

		const off = content.currentCenter - borderThickness - content.nonAnimHeight / 2;
		const diff = parent.height - Math.floor(off + content.nonAnimHeight);
		if (diff < 0)
			return off + diff;
		return Math.max(off, 0);
	}

	Behavior on offsetScale {
		Anim {
		}
	}
	Behavior on x {
		enabled: !root.horizontal || root.offsetScale < 1

		Anim {
			duration: content.animLength
			easing: content.animCurve
		}
	}
	Behavior on y {
		enabled: root.horizontal || root.offsetScale < 1

		Anim {
			duration: content.animLength
			easing: content.animCurve
		}
	}

	Wrapper {
		id: content

		anchors.bottom: root.position === "bottom" ? parent.bottom : undefined
		anchors.bottomMargin: (-implicitHeight - 5) * root.offsetScale
		anchors.horizontalCenter: root.horizontal ? parent.horizontalCenter : undefined
		anchors.left: root.horizontal ? undefined : parent.left
		anchors.leftMargin: (-implicitWidth - 5) * root.offsetScale
		anchors.top: root.position === "top" ? parent.top : undefined
		anchors.topMargin: (-implicitHeight - 5) * root.offsetScale
		anchors.verticalCenter: root.horizontal ? undefined : parent.verticalCenter
		horizontal: root.horizontal
		offsetScale: root.offsetScale
		screen: root.screen
	}
}
