--[[
	Returns the enums.PrimaryButtonModes that is reflective of the ownership and
	equip status from the specified `assetId` and `assetType` of a MerchBooth
	accessory.

	Default mode is Purchase. Ownership is checked through
	MarketplaceService:PlayerOwnsAsset; if the accessory is owned, then equip
	status is then checked through the player's HumanoidDescription first and
	the humanoid's accessories second.

	This function is called when the MerchBooth is opened.
]]


local MarketplaceService = game:GetService("MarketplaceService")

local MerchBooth = script:FindFirstAncestor("MerchBooth")

local enums = require(MerchBooth.enums)
local types = require(MerchBooth.types)
local isItemEquipped = require(MerchBooth.Modules.isItemEquipped)

return function(mockMarketplaceService: MarketplaceService)
	MarketplaceService = mockMarketplaceService or MarketplaceService

	local function getButtonModeAsync(player: Player, itemInfo: types.ItemInfo): string
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildOfClass("Humanoid")
		local mode = enums.PrimaryButtonModes.Purchase

		if humanoid then
			if itemInfo.isOwned then
				mode = enums.PrimaryButtonModes.Owned

				--if itemInfo.assetType then
				--	mode = enums.PrimaryButtonModes.CanEquip
				--	local description = humanoid:GetAppliedDescription()
				--	if isItemEquipped(description, itemInfo) then
				--		mode = enums.PrimaryButtonModes.Equipped
				--	end
				--end
			elseif itemInfo.price < 0 then
				mode = enums.PrimaryButtonModes.NotForSale
			end
		end

		return mode
	end

	return getButtonModeAsync
end
