--[[----------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

	-- WF.core.utils
local WFArguments = require 'WFArguments'

	-- WF.web.utils
local WFURI = require 'WFURI'

--------------------------------------------------------------------------------

local check = assert( WFArguments.check )
local parse = assert( WFURI.parse )
local resolve = assert( WFURI.resolve )

--==============================================================================

local WFCatUrlUtils = {}

--------------------------------------------------------------------------------

local function _resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl )

	if maybeRelativeUrl:match( 'http://' ) or maybeRelativeUrl:match( 'https://' ) then
		return maybeRelativeUrl
	end
	
	local newHref = resolve( baseUrl, maybeRelativeUrl )
	
	if newHref then
	
		local urlInfo = parse( newHref )
		
		local query = urlInfo.query
		
		if query then
			return string.format( '%s?%s', urlInfo.path, query )
		else
			return urlInfo.path
		end
		
	end

end

--------------------------------------------------------------------------------
--[[!

	\brief Resolves to an absolute url given a pair of base and relative url 
	
		Sample: 
		
		baseUrl: https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/
		maybeRelativeUrl: assets/aadec352a4ae46408d50b4e2bb7d34c8
		
		Result: /v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8
	
		If the maybeRelativeUrl is already absolute or a full URL including schema
		and host, maybeRelativeUrl is returned without any changes made
		
	\param baseUrl (string) The base url.
	\param maybeRelativeUrl (string) Maybe relative url.

	\return Absolute url
	
--]]

function WFCatUrlUtils.resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl )

	check( '+string', 'string' )
	
	return _resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl )
	
end

--------------------------------------------------------------------------------
--[[!

	\brief Resolves an entire links table to absolute urls given the base and relative url 
	
		Sample: 
		
		baseUrl: https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/
		
		links = {
			[ '/rels/rendition_type/thumbnail2x' ] = {
				href = 'assets/aadec352a4ae46408d50b4e2bb7d34c8/renditions/008d8ccf9ca64559b2d6c4ba8c85fefe'
			}
		}
		
		Result:
		
		links = {
			[ '/rels/rendition_type/thumbnail2x' ] = {
				href = '/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b
					/assets/aadec352a4ae46408d50b4e2bb7d34c8/renditions/008d8ccf9ca64559b2d6c4ba8c85fefe'
		}
	
		If the href in the link is already absolute or a full URL including schema
		and host, the links isn't modified
		
	\param baseUrl (string) The base url.
	\param links (table) Links table ( e.g. as returned by Oz in the asset document )

	\return links table with absolute href's
	
--]]

function WFCatUrlUtils.resolveLinksToAbsoluteUrls( baseUrl, links )

	check( '+string', 'table|nil')
	
	if links then
	
		for _, v in pairs( links ) do
				
			local href = v.href
			
			if href then
			
				v.href = _resolveToAbsoluteUrl( baseUrl, href )
				
			end
			
		end
		
	end
	
	return links
	
end

--------------------------------------------------------------------------------

return WFCatUrlUtils
