local q=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local Q=q:WaitForChild("\077\111\100\117\108\101\115")local R=((game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")):WaitForChild("\083\116\097\114\116\101\114\080\108\097\121\101\114\083\099\114\105\112\116\115")).clientSrc local j=require(Q.M)local E=require(Q.Roact)local x=require(Q.src.Time)local F=require(R.Components.common.TextLabel)local J=E.createElement local S=E.PureComponent:extend("\084\105\109\101\114")function S.init(q)q:setState({currentTime=q.props.initialTime or 0})end function S.didMount(q)q.running=true spawn(function()while q.running do q:setState(function(Q)local R=q.props.key==Q.key if R then return{currentTime=Q.currentTime+1;initialTime=q.props.initialTime}else return{currentTime=0;initialTime=q.props.initialTime,key=q.props.key}end end)wait(1)end end)end function S.willUnmount(q)q.running=false end function S.render(q)local Q=q.props local R=Q.initialTime local E=Q.increment local S=R-os.time()if E then S=os.time()-R end return J(F,j.extend(j.omit(Q,"\105\110\099\114\101\109\101\110\116","\105\110\105\116\105\097\108\084\105\109\101","\107\101\121"),{Text=x.FormatTime(S)}))end return S
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
-- local logger = require(Modules.src.utils.Logger)
local clientSrc = game:GetService('StarterPlayer'):WaitForChild('StarterPlayerScripts').clientSrc

local M = require(Modules.M)
local Roact = require(Modules.Roact)

local Time = require(Modules.src.Time)

local TextLabel = require(clientSrc.Components.common.TextLabel)

local createElement = Roact.createElement
local Timer = Roact.PureComponent:extend('Timer')

function Timer:init()
	self:setState({ currentTime = self.props.initialTime or 0 })
end

function Timer:didMount()
	self.running = true

	-- We don't want to block the main thread, so we spawn a new one!
	spawn(function()
		while self.running do
			self:setState(function(state)
				local updateTime = self.props.key == state.key
				if updateTime then
					return {
						currentTime = state.currentTime + 1,
						initialTime = self.props.initialTime,
					}
				else
					return {
						currentTime = 0,
						initialTime = self.props.initialTime,
						key = self.props.key,
					}
				end
			end)
			wait(1)
		end
	end)
end

function Timer:willUnmount()
	self.running = false
end

function Timer:render()
	local props = self.props
	local initialTime = props.initialTime
	local increment = props.increment
	local duration = initialTime - os.time()

	if increment then
		duration = os.time() - initialTime
	end

	return createElement(
		TextLabel,
		M.extend(M.omit(props, 'increment', 'initialTime', 'key'), {
			Text = Time.FormatTime(duration),
		})
	)
end

return Timer