--[[
	@class stringTween
	contains library of functions that add/remove text from a string.
	
	.backward:
		@params: string, function, string?, config?
     	@info: clears text or until hit `StopAt`		
	
	.forward: 
		@params: string, string, function, config?
		@info: adds `newText` to the given string

	```lua
		local stringTween = require(container.stringTween)

		local startText = 'person'
		local newText = 'son'
		local stopAt = 'per'		

		local predicate = function(text)
			startText = text
		end

		stringTween.backward(startText, predicate, stopAt) > returns per
		stringTween.forward(startText, newText, predicate) > returns person
	```
]]


local stringTween = {}

function stringTween.forward(
	s: string, 
	apply: string, 
	predicate: (string) -> (), 
	config: config?
)
	if config then
		config.delay = config.delay or defaultconfig.delay
		config.movement = config.movement or defaultconfig.movement
	end

	local config = config or defaultconfig
	local result
	
	for i = 0, #apply, 1 do
		result = `{s}{string.sub(apply, 1, i)}{config.movement}`
		
		predicate(result)
		
		task.wait(config.delay)
	end
	
	result = result:sub(1, #result - #config.movement)
	predicate(result)
	
	return result
end

function stringTween.backward(
	s: string, 
	predicate: (string) -> (), 
	breakat: string?, 
	config: config?
)
	if config then
		config.delay = config.delay or defaultconfig.delay
		config.movement = config.movement or defaultconfig.movement
	end
	
	local config = config or defaultconfig
	local result
	
	for i = #s, 0, -1 do
		result = `{string.sub(s, 1, i)}{config.movement}`
		predicate(result)
		
		if breakat and string.sub(result, 1, -1) == `{breakat}{config.movement}` then
			break 
		end
		task.wait(config.delay)
	end
	
	result = string.sub(result, 1, #result - #config.movement)
	predicate(result)
	
	return result
end

return stringTween
