-- lx-bundle:true

local common = require("core.common")
local config = require("core.config")
local style = require("core.style")

local messages = {}

-- Compute status-popup visibility time from message length
local function compute_message_timeout(text)
	local min_t = common.clamp(tonumber(config.message_timeout_min) or 1, 1, 5)
	local max_t = common.clamp(tonumber(config.message_timeout_max) or 5, 1, 5)
	if max_t < min_t then
		min_t, max_t = max_t, min_t
	end

	local min_chars = math.max(0, math.floor(tonumber(config.message_timeout_min_chars) or 15))
	local max_chars = math.max(1, math.floor(tonumber(config.message_timeout_max_chars) or 80))
	if max_chars < min_chars then
		min_chars, max_chars = max_chars, min_chars
	end

	local len = #tostring(text or "")
	if len <= min_chars then
		return min_t
	end
	if len >= max_chars then
		return max_t
	end
	local span = math.max(1, max_chars - min_chars)
	local ratio = (len - min_chars) / span
	return min_t + (max_t - min_t) * ratio
end

function messages.build_message(icon, icon_color, text, separator2)
	return {
		icon_color,
		style.icon_font,
		icon,
		style.dim,
		style.font,
		separator2 or "  |  ",
		style.text,
		text,
	}
end

function messages.get_timeout_duration(text)
	return compute_message_timeout(text)
end

function messages.show_info(text, separator2)
	return messages.build_message("i", style.text, text, separator2)
end

function messages.show_success(text, separator2)
	return messages.build_message("i", style.success or style.text, text, separator2)
end

function messages.show_error(text, separator2)
	return messages.build_message("!", style.error or style.text, text, separator2)
end

function messages.show_warning(text, separator2)
	return messages.build_message("i", style.warning or style.text, text, separator2)
end

function messages.show_notice(text, separator2)
	return messages.build_message("i", style.notice or style.text, text, separator2)
end

return messages
