import Quickshell
import QtQuick
import ZShell.Components
import ZShell.Config
import qs.Modules.Launcher.Items
import qs.Components
import qs.Helpers

CarouselView {
	id: root

	required property var panels
	readonly property var screen: QsWindow.window?.screen
	required property SearchBar search
	readonly property var values: Wallpapers.query(root.search.text.split(" ").slice(1).join(" "))
	required property var visibilities

	currentIndex: {
		const idx = Wallpapers.list.findIndex(w => w.path === Wallpapers.actualCurrent);
		return idx >= 0 ? idx : 0;
	}
	delegateProperties: ({
			visibilities: root.visibilities
		})
	focalWidth: Config.launcher.sizes.wallpaperWidth
	glideDuration: 250
	glideEasingType: Easing.OutCubic
	implicitHeight: tileHeight + Tokens.spacing.small / 2 + Tokens.padding.large + Tokens.padding.small
	itemSpacing: Tokens.spacing.small
	maxWidth: {
		if (!screen)
			return 0;
		const barMargins = Config.bar.border * 2;
		let margins = Math.round(screen.width / 6);
		if (visibilities.utilities || visibilities.sidebar)
			margins = Config.sidebar.sizes.width;
		return screen.width - (barMargins + margins) * 2 - Config.bar.rounding * 4;
	}
	minEdgeWidth: 56
	model: values
	tileHeight: focalWidth / 16 * 9

	delegate: Component {
		WallpaperTile {
		}
	}

	Component.onDestruction: Wallpapers.stopPreview()
	onCurrentItemChanged: {
		if (currentItem)
			Wallpapers.preview(currentItem.modelData.path);
	}
}
