local x=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local C=x.AvatarEditor local I=require(C.Client.GuiLib.LazyLoader)local N=require(C.Shared.Util.Maid)local X=require(C.Shared.Util.Signal)local a=require(C.Shared.Util.Promise)local O=require(C.Shared.Settings)local q={}q.__index=q function q.new(x,C)local a=setmetatable({},q)a.State=C a.SearchChanged=X.new()a.Maid=N.new()a.NextSearch=nil a.SearchPromise=nil a.Frame=x local O=x.TextBox local c=I.Classes.TextMask.new(O)a.Maid:GiveTask(c)c:SetMaskType("\083\116\114\105\110\103")c:SetMaxLength(50)a.Maid:GiveTask(c.Frame.FocusLost:Connect(function(x,C)if not x then return end local I=c:GetValue()a:OnSearchChanged(I,true)end))a.Maid:GiveTask((c.Frame:GetPropertyChangedSignal("\084\101\120\116")):Connect(function()local x=c:GetValue()a:OnSearchChanged(x)end))a.Maid:GiveTask(x.Clear.Activated:Connect(function(x)O.Text=""end))return a end function q.OnSearchChanged(I,x,C)local N=I.State local X=I.SearchPromise I.NextSearch=x if X then X:cancel()end I.SearchPromise=(a.new(function(x,C,X)(a.delay(O.SEARCH_DELAY)):andThen(function()if not X(function() end)then if N.search==I.NextSearch then C("\115\097\109\101 \115\101\097\114\099\104")else N.search=I.NextSearch I.SearchChanged:Fire()x()end end end)end)):catch(function() end)end function q.Destroy(x)x.State=nil x.SearchChanged:Destroy()x.SearchChanged=nil x.Maid:DoCleaning()x.Maid=nil x.NextSearch=nil x.SearchPromise=nil x.Frame=nil setmetatable(x,nil)end return q
-- Class
-- 0_1195
-- January 10, 2021

--[[



]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AvatarEditor = ReplicatedStorage.AvatarEditor
local GuiLib = require(AvatarEditor.Client.GuiLib.LazyLoader)
local Maid = require(AvatarEditor.Shared.Util.Maid)
local Signal = require(AvatarEditor.Shared.Util.Signal)
local Promise = require(AvatarEditor.Shared.Util.Promise)
local Settings = require(AvatarEditor.Shared.Settings)

local Class = {}
Class.__index = Class


function Class.new(frame, searchState)
	local self = setmetatable({}, Class)
	
	self.State = searchState
	self.SearchChanged = Signal.new()
	self.Maid = Maid.new()
	self.NextSearch = nil
	self.SearchPromise = nil
	self.Frame = frame
	
	local searchBox = frame.TextBox
	
	local mask = GuiLib.Classes.TextMask.new(searchBox)
	self.Maid:GiveTask(mask)

	mask:SetMaskType("String")
	mask:SetMaxLength(50)
	
	self.Maid:GiveTask(mask.Frame.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLoss)
		if not enterPressed then
			return
		end
		local value = mask:GetValue()
		self:OnSearchChanged(value, true)
	end))

	self.Maid:GiveTask(mask.Frame:GetPropertyChangedSignal("Text"):Connect(function()
		local value = mask:GetValue()
		self:OnSearchChanged(value)
	end))

	self.Maid:GiveTask(frame.Clear.Activated:Connect(function(inputObject)
		searchBox.Text = ""
	end))
	
	return self
end


function Class:OnSearchChanged(pattern, focusLost)
	local state = self.State
	local searchPromise = self.SearchPromise
	
	self.NextSearch = pattern

	if searchPromise then
		searchPromise:cancel()
	end
	-- cant believe this works
	self.SearchPromise = Promise.new(function(resolve, reject, onCancel)
		Promise.delay(Settings.SEARCH_DELAY):andThen(function()
			if not onCancel(function() end) then
				if state.search == self.NextSearch then
					reject("same search")
				else
					state.search = self.NextSearch
					self.SearchChanged:Fire()
					resolve()
				end
			end
		end)
	end):catch(function()

	end)
end


function Class:Destroy()
	self.State = nil
	self.SearchChanged:Destroy()
	self.SearchChanged = nil
	self.Maid:DoCleaning()
	self.Maid = nil
	self.NextSearch = nil
	self.SearchPromise = nil
	self.Frame = nil
	setmetatable(self, nil)
end


return Class