-- lx-bundle:true

--- Commands shared by rendered-document previews
-- @module core.preview.commands

local command = require("core.command")
local core = require("core")
local keymap = require("core.keymap")
local preview_export = require("core.preview.export")

--- Return whether commands currently target a rendered-document preview
-- @return boolean active true when focused view uses core preview
local function is_active_document_preview()
	local active_view = core.active_view

	return active_view and active_view.is_document_preview == true or false
end

--- Return whether the active preview supplies an export view
-- @return boolean active true when generic PNG export is available
local function is_exportable_preview()
	local active_view = core.active_view

	return active_view and type(active_view.create_export_view) == "function" or false
end

command.register({
	-- Let each format adapter decide how its retained targets should open
	["preview:open-link"] = {
		predicate = is_active_document_preview,
		perform = function(context)
			if type(context) ~= "table" then
				return false
			end

			return core.active_view:open_link_at(context.mouse_x, context.mouse_y)
		end,
	},
	-- Move through format-neutral rendered document rows and pages
	["preview:scroll-up"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_vertical_step(-1)
			return true
		end,
	},
	["preview:scroll-down"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_vertical_step(1)
			return true
		end,
	},
	["preview:page-up"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_page(-1)
			return true
		end,
	},
	["preview:page-down"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_page(1)
			return true
		end,
	},
	["preview:scroll-left"] = {
		predicate = is_active_document_preview,
		perform = function()
			return core.active_view:scroll_horizontal_page(-1)
		end,
	},
	["preview:scroll-right"] = {
		predicate = is_active_document_preview,
		perform = function()
			return core.active_view:scroll_horizontal_page(1)
		end,
	},
	["preview:home"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_to_vertical_edge(false)
			return true
		end,
	},
	["preview:end"] = {
		predicate = is_active_document_preview,
		perform = function()
			core.active_view:scroll_to_vertical_edge(true)
			return true
		end,
	},
	-- Render the complete document as numbered fixed-size PNG pages beside its source
	["preview:render-images"] = {
		predicate = is_exportable_preview,
		perform = function()
			return preview_export.start(core.active_view)
		end,
	},
})

-- Keep format-neutral reading controls available to every direct core preview
keymap.bind("plugin", {
	document_preview = {
		["lclick"] = { "preview:open-link", "doc:mouse-pan" },
		["up"] = "preview:scroll-up",
		["down"] = "preview:scroll-down",
		["shift+space"] = "preview:page-up",
		["space"] = "preview:page-down",
		["pageup"] = "preview:page-up",
		["pagedown"] = "preview:page-down",
		["shift+left"] = "preview:scroll-left",
		["shift+right"] = "preview:scroll-right",
		["home"] = "preview:home",
		["end"] = "preview:end",
		["ctrl+home"] = "preview:home",
		["ctrl+end"] = "preview:end",
		["+"] = "zoom:in",
		["-"] = "zoom:out",
		["0"] = "zoom:reset",
		["keypad +"] = "zoom:in",
		["keypad -"] = "zoom:out",
		["keypad *"] = "zoom:reset",
	},
})

return true
