local K=require(script.Parent.Parent.Promise)local P=require(script.Parent._config)local U=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local O=require(script.Parent.Util.withClassName)local c=require(script.Parent.Query)local N={}function N.class(N)local W={}function W.create(W,c)return K.new(function(K,W)local y=O(N)local t=U:RequestAsync({Url=y;Method="\080\079\083\084",Headers=P:getHeaders(),Body=U:JSONEncode(c)})if t.Success then K(U:JSONDecode(t.Body))return end W(U:JSONDecode(t.Body))end)end function W.get(W,c)return K.new(function(K,W)local y=O(N,c)local t=U:RequestAsync({Url=y;Method="\071\069\084",Headers=P:getHeaders()})if t.Success then K(U:JSONDecode(t.Body))return end W(U:JSONDecode(t.Body))end)end function W.update(y,c,W)return K.new(function(K,y)local t=O(N,c)local H=U:RequestAsync({Url=t;Method="\080\085\084";Body=U:JSONEncode(W),Headers=P:getHeaders()})K(U:JSONDecode(H.Body))end)end function W.query(K)return c.new(N)end function W.delete(W,c)return K.new(function(K,W)local y=O(N,c)local t=U:RequestAsync({Url=y,Method="\068\069\076\069\084\069";Headers=P:getHeaders()})K(U:JSONDecode(t.Body))end)end return W end return N
local Promise = require(script.Parent.Parent.Promise)
local _config = require(script.Parent._config)
local HttpService = game:GetService("HttpService")

local withClassName = require(script.Parent.Util.withClassName)


local Query = require(script.Parent.Query)

local Objects = {}

function Objects.class(className)	
	local self = {}
	
	function self:create(object)
		return Promise.new(function(resolve, reject)
			local url = withClassName(className)
			local response = HttpService:RequestAsync({
				Url = url,
				Method = "POST",
				Headers = _config:getHeaders(),
				Body = HttpService:JSONEncode(object)
			})
			
			if response.Success then
				resolve(HttpService:JSONDecode(response.Body))
				return
			end

			reject(HttpService:JSONDecode(response.Body))
		end)
	end
	
	function self:get(objectId)
		return Promise.new(function(resolve, reject)
			local url = withClassName(className, objectId)
			local response = HttpService:RequestAsync({
				Url = url,
				Method = "GET",
				Headers = _config:getHeaders()
			})
			
			if response.Success then
				resolve(HttpService:JSONDecode(response.Body))
				return
			end
			
			reject(HttpService:JSONDecode(response.Body))
		end)
	end
	
	function self:update(objectId, updateDocument)
		return Promise.new(function(resolve, reject)
			local url = withClassName(className, objectId)
			local response = HttpService:RequestAsync({
				Url = url,
				Method = "PUT",
				Body = HttpService:JSONEncode(updateDocument),
				Headers = _config:getHeaders()
			})
			
			resolve(HttpService:JSONDecode(response.Body))
		end)
	end
	
	function self:query()
		return Query.new(className)
	end
	
	function self:delete(objectId)
		return Promise.new(function(resolve, reject)
			local url = withClassName(className, objectId)
			local response = HttpService:RequestAsync({
				Url = url,
				Method = "DELETE",
				Headers = _config:getHeaders()
			})

			resolve(HttpService:JSONDecode(response.Body))
		end)
	end
	
	return self
end

return Objects
