local p = {}
local wl= require('وحدة:ص.م')
local wd = require('Module:Wikidata2').formatStatementsFromLua
local wd2 = require('Module:Wikidata2').formatEntityId

local function mixline( s, ss )
	return wl.MixedLine({s,ss,ss})
end
function p.pageId(frame)
	if not mw.wikibase then return nil end
	entity = mw.wikibase.getEntityObject()
	if not entity 
	then 
		return nil
	else 
		return entity.id
	end
end

function doo( prop, entityclaims, args )
	if args.noref then ref = '' else ref = 't' end
	if args.noquall and  args.noquall ~='' then quall = '' else quall = 't' end
	--------------------
	if mw.wikibase then
		paa = mw.wikibase.label( prop ) --wd2(prop, {nolink = 'true',enlabelcate ='t'}).value
	else
		paa = prop
	end
	-- pe =  wd({property = 'P1629', entityId = prop , firstvalue= 'true', noref = 'true', label = wd2(prop, {nolink = 'true'}).value}) 
	--------------------
	po = wd( {property = prop,image='image',size='120px',pattern ='autourl2',addTrackingCat ='t',noicon='t'
		,quaall=quall,reff=ref, enlabelcate ='t',showlang='t'}, entityclaims ) --formatStatement( statement, args )
	--------------------
	if po and po ~='' then
		vaa = wl.MixedLine({paa,po,po})-- mixline(paa,po)
	end
	--------------------
	if tostring(mw.getCurrentFrame():preprocess(vaa)):find('scribunto-error') then
    	return ''
	else
    	return vaa
	end
	--------------------
end
function barexternal( entity, args )
	if args.external and args.external ~= '' then
		if args.external =='no'
		then return { external = '' }
		end
	end
	local external = {}
	for i, ss in pairs( entity.claims ) do
		local entity1 = mw.wikibase.getEntityObject( i )
		if entity1.datatype == 'external-id' 
				then
					if args[1] and args[1] ~= ''
					then
						if i == args[1] then vaa = doo( i, entity.claims, args ) else  vaa = ''  end
					else
						vaa = doo( i, entity.claims, args )	
					end
					table.insert( external, vaa )
			end
	end
	final = table.concat(external)
	return { external = final }
end

function barother( entity, args )
	if args.external and args.external ~= '' then
		if args.external =='yes'
		then return { other =  '' }
		end
	end	
	local other = {}
	for i, ss in pairs( entity.claims ) do
		local entity1 = mw.wikibase.getEntityObject( i )
		if entity1.datatype == 'external-id' 
				then
					v = ''
				else
					if args[1] and args[1] ~= ''
					then
						if i == args[1] then vaa = doo( i, entity.claims, args ) else  vaa = ''  end
					else
						vaa = doo( i, entity.claims, args )	
					end
					table.insert( other, vaa )
			end
	end
	final = table.concat(other)
	return { other =  final }
end

function fStatement( entity, args )
		if (not entity or not entity.claims) then return '' end --TODO error? 
		local pp = ''
		if p.pageId() then
			pp = p.pageId()
		end
		local ids = args.entityId or args.id
		local pageId = ""
		if ids and ids ~= "" then pageId = ids else pageId = pp end
		local tit = ''
		if mw.wikibase then tit = mw.wikibase.label(pageId) else tit = pageId end
		local title2 = tit .. ' [[ملف:Wikidata-logo.svg|20px|baseline|link=d:'.. pageId ..']]'
		local Open = wl.Open({id = ''})
		local title = wl.Title({title2,''})
		local closse = '\n</table>'  
		local foo = barother( entity, args ).other 
		local bar = barexternal( entity, args ).external
		local subt = wl.SubTitle({'معرفات خارجية','معرفات خارجية' })
       	local fs = {}
			table.insert( fs, Open )
			table.insert( fs, title )
			table.insert( fs, foo )
			if bar and bar ~='' then
				table.insert( fs, subt)
				table.insert( fs, bar )
			end
			table.insert( fs, closse )
	return table.concat(fs) --, ' \n*'
end

function p.f( frame, key )
	    local args = frame.args
		local entity = nil
    	if entity and type( entity ) == "table" then
        	entity = entity
    	elseif args.id and args.id ~= "" then
        	entity = mw.wikibase.getEntityObject( args.id )
    	else
        	entity = mw.wikibase.getEntityObject(  )
    	end

	return fStatement(entity, args )
end
return p