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



]]

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
