-- clothes_toggle.lua
-- Enforces lewd_noita.clothes_enabled on the player dress sprite.
-- Attached to player_base.xml; also callable via apply_player_clothes_setting().

function apply_player_clothes_setting(player_id)
	if player_id == nil or player_id == 0 then
		player_id = EntityGetWithTag("player_unit")[1]
	end
	if player_id == nil or player_id == 0 then return end

	local clothes_enabled = ModSettingGet("lewd_noita.clothes_enabled")
	if clothes_enabled == nil then clothes_enabled = true end

	for _, comp in ipairs(EntityGetComponent(player_id, "SpriteComponent") or {}) do
		local is_dress = ComponentHasTag(comp, "ln_dress")
		if not is_dress then
			local image_file = ComponentGetValue2(comp, "image_file") or ""
			is_dress = image_file:find("player_dress", 1, true) ~= nil
		end
		if is_dress then
			-- Use visible (same as controls_toggle.lua); disabling the component alone
			-- is unreliable on the player entity.
			ComponentSetValue2(comp, "visible", clothes_enabled)
			EntitySetComponentIsEnabled(player_id, comp, clothes_enabled)
		end
	end
end

apply_player_clothes_setting(GetUpdatedEntityID())
