local p = {}

local function trim(s)
	--Removes leading zeroes from value
	if s:sub(1,1)=="P" then return s:sub(2)
	else return s
	end
end

local function xt(e)
	return  'صفحات تستخدم خاصية '.. e
end

local function wd(e)
	return e .. ' من ويكي بيانات'
end

local function list(s)
	local I = trim(s)
	return xt(s)
end

local function category(propertyid,q,nocate)
    local main = '[[تصنيف:صفحات بها بيانات ويكي بيانات|'.. trim(propertyid) ..']]'
	if nocate and nocate  ~= ''  
		then -- to hide the category
	 		return ''
	 	elseif q and q  ~= ''  
		then 
	 		return q..''..main 
	end
	local d = mw.text.trim(propertyid)
	local s = list(d)
	if s then
		return '[[تصنيف:'.. s ..']]'..main  -- linktext(s)
	end
end

local function icons(u,id,noicon,icon2)
	if noicon and noicon  ~= ''  
		then -- to hide the icon
	 		return ''
	end
	local label = ""--mw.wikibase.label(u) --{{#invoke:Wikibase|label| u }}
	--if label ==nil or not label  then label ='' else end

	local temp =  label .. ' ('.. u ..')'
	local ico =  'Twemoji_270f.svg|13px'
		if icon2 and icon2 ~='' then 
			ico = 'Wikidata-logo.svg|20px' 
			size = '20px' 
			end
	local so = 'تعديل قيمة خاصية ' .. temp ..' في ويكي بيانات'
	local im =' [[ملف:'.. ico .. '|baseline|link=d:'.. (id or '') .. '#'.. u ..'|'.. so .. ']]'
	local noprint=  '<span class="noprint">'.. im ..'</span>'
	local sup=  '<sup>'.. noprint ..'</sup>'
	if u then
		return noprint
	end
end

function p.pageId(frame)
	return mw.wikibase.getEntityIdForCurrentPage()
end

function p.makecategory1(options)  --Main function
	local page_title_object = mw.title.getCurrentTitle()
	local ns = page_title_object.namespace	
	local title = page_title_object.text
	-- ############################
	local prop = options['property']
	local prop2 = options['justthisqual']
	local noicon = options['noicon'] -- options to hide the icon.
	local nocate = options['nocate']  -- options to hide the category.
	local q = options['category']
	local iid = options['entityId'] or options['id']
	local icon2 = options['icon2']
	-- ############################
	local id
    if iid == '' or not iid then
        id = p.pageId()
    else
        id = iid
    end
	-- ############################
	if prop  == '' or not prop then
		return nil -- don't do anything if no options property.
	end
	-- ############################
	local propertyid = mw.ustring.gsub(prop:upper(), " ", "")

	local icon = icons(propertyid,id,noicon,icon2)   -- function to display the icon.
	local cate = category(propertyid,q,nocate) -- function to display the category.
	-- ############################
	if prop2 and prop2 ~= "" then 
		cate = cate .. category( mw.ustring.gsub(prop2:upper(), " ", "") , q , nocate) 
	end
	-- ############################
	local last = cate ..''.. icon
	--mw.log("Module:Wikidata/تتبع:" .. title)
	if title == "ويكي بيانات/ملعب ويكي بيانات" then
		last = icon
	end	
	if ns == 2 then -- to hide category in user pages
		last = icon
	end
	return last
end

function p.makecategory(frame)  --Main function
	local final = p.makecategory1(frame.args)
	return final
end

function p.make1(prop,id)  --Main function
	if id == '' or not id then
             id = p.pageId()
             else id = iid 
      end
	if prop  == '' or not prop then
		return nil -- don't do anything if no args property.
	end
	local propertyid = mw.ustring.gsub(prop:upper(), " ", "")

	local icon = icons(propertyid,id,noicon)   -- function to display the icon.
	local cate = category(propertyid,q,nocate) -- function to display the category.
	
	local last = cate ..''.. icon
	return last
end


function p.SS(frame) --testing function
	local s = mw.ustring.gsub(frame.args['property']:upper(), " ", "")
	s = list(s)
	s = mw.text.trim(s)
	if s  == '' or not s then
		return  nil
	end
	local q = frame.args['category']
	if q  == '' or not q then
		return category(s)
        else return q
	end
end

return p