--!strict
-- BloxTrack's client part (optional). https://bloxtrack-app.vercel.app/docs
--
-- A ModuleScript: put it in ReplicatedStorage, name it "BloxTrackClient", and require it once from a LocalScript in
-- StarterPlayerScripts. Requiring it tells the server this player's device (phone, tablet, desktop, console), so
-- session_start carries it. LocalScripts can also send events, but only the names the server lists in
-- BloxTrack.Start's `clientEvents` (and at most 60 a minute per player):
--
--     local BloxTrackClient = require(game:GetService("ReplicatedStorage"):WaitForChild("BloxTrackClient"))
--     BloxTrackClient.Track("menu_open", "Shop")
--
-- Never put an API key here: everything in ReplicatedStorage can be read by players.

local GuiService = game:GetService("GuiService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local remote = ReplicatedStorage:WaitForChild("BloxTrackRemote") :: RemoteEvent

local BloxTrackClient = {}

-- A touch screen without a keyboard is a phone or a tablet (by its shorter side), a TV-style screen is a console,
-- anything else a desktop.
local function device(): string
	if GuiService:IsTenFootInterface() then
		return "console"
	end
	if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled then
		local camera = workspace.CurrentCamera
		local size = if camera then camera.ViewportSize else Vector2.new(1920, 1080)
		return if math.min(size.X, size.Y) < 600 then "phone" else "tablet"
	end
	return "desktop"
end

--- Sends an event for this player. name must be in the server's clientEvents list, or it's ignored.
function BloxTrackClient.Track(name: string, key: (string | number)?, value: number?, props: { [string]: any }?)
	remote:FireServer("event", name, key, value, props)
end

remote:FireServer("device", device())

return BloxTrackClient
