local t=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local H=t.AvatarEditor local u=require(H.Client.GuiLib.LazyLoader)local I=require(H.Shared.Util.Maid)local w=require(H.Shared.Util.Signal)local i=require(H.Shared.Util.Promise)local G=require(H.Shared.Settings)local Z={}Z.__index=Z function Z.new(t,H)local i=setmetatable({},Z)i.State=H i.SearchChanged=w.new()i.Maid=I.new()i.NextSearch=nil i.SearchPromise=nil i.Frame=t local G=t.TextBox local k=u.Classes.TextMask.new(G)i.Maid:GiveTask(k)k:SetMaskType("\083\116\114\105\110\103")k:SetMaxLength(50)i.Maid:GiveTask(k.Frame.FocusLost:Connect(function(t,H)if not t then return end local u=k:GetValue()i:OnSearchChanged(u,true)end))i.Maid:GiveTask((k.Frame:GetPropertyChangedSignal("\084\101\120\116")):Connect(function()local t=k:GetValue()i:OnSearchChanged(t)end))i.Maid:GiveTask(t.Clear.Activated:Connect(function(t)G.Text=""end))return i end function Z.OnSearchChanged(u,t,H)local I=u.State local w=u.SearchPromise u.NextSearch=t if w then w:cancel()end u.SearchPromise=(i.new(function(t,H,w)(i.delay(G.SEARCH_DELAY)):andThen(function()if not w(function() end)then if I.search==u.NextSearch then H("\115\097\109\101 \115\101\097\114\099\104")else I.search=u.NextSearch u.SearchChanged:Fire()t()end end end)end)):catch(function() end)end function Z.Destroy(t)t.State=nil t.SearchChanged:Destroy()t.SearchChanged=nil t.Maid:DoCleaning()t.Maid=nil t.NextSearch=nil t.SearchPromise=nil t.Frame=nil setmetatable(t,nil)end return Z


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
