-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

--- Markdown preparation for shared fixed-page PNG export
-- @module plugins.mdpreview.export

local common = require("core.common")
local config = require("core.config")
local markdown_design = require("plugins.mdpreview.design")

local export = {}

config.plugins.mdpreview = common.merge({
	-- PNG export uses stable page dimensions independently of window size
	export_width = 2560,
	export_height = 3840,
	-- Auto preserves live document scale after fitting it to export width
	export_scale = "auto",
},
config.plugins.mdpreview)

--- Resolve final export zoom from configured scale and live document width
-- @param view table live Markdown preview
-- @param export_width number output page width
-- @return number scale final export zoom multiplier
function export.resolve_scale(view, export_width)
	local configured_scale = tonumber((config.plugins.mdpreview or {}).export_scale)

	if configured_scale and configured_scale > 0 then
		return configured_scale
	end

	local live_width = math.max(1, view:get_document_viewport_width())
	local live_scale = tonumber(view.zoom_multiplier) or 1

	-- Scaling width and text equally preserves live line wrapping on wider pages
	return live_scale * export_width / live_width
end

--- Add fixed-page export support to Markdown preview class
-- @param MarkdownPreview table Markdown preview class
-- @param load_preview_fonts function font loader shared with live previews
function export.install(MarkdownPreview, load_preview_fonts)
	-- Shared pagination needs Markdown's document padding beside its measured rows
	MarkdownPreview.preview_design = markdown_design

	--- Create detached fully measured preview for one export
	-- @return table view detached Markdown preview
	-- @return number width output page width
	-- @return number height output page height
	function MarkdownPreview:create_export_view()
		local plugin_config = config.plugins.mdpreview or {}
		local export_width = math.max(1, math.floor(tonumber(plugin_config.export_width) or 2560))
		local export_height = math.max(1, math.floor(tonumber(plugin_config.export_height) or 3840))
		local export_scale = export.resolve_scale(self, export_width)
		local export_view = MarkdownPreview(self.doc)

		-- Detached view reflows document at page width without changing live preview state
		export_view.position.x = 0
		export_view.position.y = 0
		export_view.size.x = export_width
		export_view.document_viewport_width_override = export_width
		export_view.zoom_multiplier = export_scale
		export_view.preview_fonts = load_preview_fonts(export_scale)
		-- Tall temporary viewport makes existing lazy layout measure every source line once
		export_view.size.y = math.max(
			export_height,
			(#self.doc.lines + 1) * export_view:get_line_height()
		)
		export_view:rebuild_layout()
		export_view.size.y = export_height
		if not export_view.layout_complete then
			error("Could not complete Markdown layout for image export", 0)
		end

		return export_view, export_width, export_height
	end
end

return export
