local U = mw.ustring.char
local gsub = mw.ustring.gsub
local export = {}

local hri = U(0x93F)
local hri2 = U(0x907)
local diri = U(0x940)
local diri2 = U(0x908)
local hru = U(0x941)
local hru2 = U(0x909)
local diru = U(0x942)
local diru2 = U(0x90A)
local E = U(0x947)
local E2 = U(0x90F)
local AI = U(0x948)
local AI2 = U(0x910)
local O = U(0x94B)
local O2 = U(0x913)
local AU = U(0x94C)
local AU2 = U(0x914)
local A = U(0x905)
local LA = U(0x93E)
local ret = U(0x615)
local halant = U(0x94D)
local zabar = U(0x64E)
local zer = U(0x650)
local pesh = U(0x64F)
local tashdid = U(0x651) -- also called shadda
local jazm = "ْ"
local he = "ہ"

local consonants = "ببپتثجچحخدذرزژسشصضطظعغفقکگلࣇمنݨوہھٹڈںڑشؕ"
local consonantS = "ببپتثجچحخدذرزژسشصضطظعغفقکگلࣇمنݨہھٹڈںڑشؕ"
local consonantS2 = "یببپتثجچحخدذرزژسشصضطظعغفقکگلࣇمنݨںوہھٹڈڑشؕ" 
local sun = "تثصشسزرذدنلطظض"
local vowels = "ایئےۓوؤ"
local hes = "ہح"
local diacritics = "َُِّْٰ"
local ZZP = "َُِ"
local hiPD = "ीूेैोौ"

local mapping = {
	["آ"] = 'आ', ["ب"] = 'ब', ["پ"] = 'प', 
    ["ت"] = 'त', ["ٹ"] = 'ट', ["ث"] = 'स',
    ["ج"] = 'ज', ["چ"] = 'च', ["ح"] = 'ह',
    ["خ"] = 'ख़', ["د"] = 'द', ["ڈ"] = 'ड', 
    ["ذ"] = 'ज़', ["ر"] = 'र', ["ڑ"] = "ड़",
    ["ز"] = 'ज़', ["ژ"] = 'श़', ["س"] = 'स',
    ["ش"] = 'श', ["ص"] = 'स', ["ض"] = 'ज़',
    ["ط"] = 'त', ["ظ"] = 'ज़', ["غ"] = 'ग़',
    ["ف"] = 'फ़', ["ق"] = 'क़', ["ک"] = 'क', 
    ["گ"] = 'ग', ["ل"] = 'ल', ["م"] = 'म', 
    ["ن"] = 'न', ["و"] = 'व', ["ہ"] = 'ह', 
    ["ی"] = 'य',
    ["ں"] = 'ं',

	["ݨ"] = 'ण', ["ࣇ"] = 'ळ', ["ك"] = 'क',

	["ع"] = 'अ',
	["ء"] = '',
	["ئ"] = '', 
	["ؤ"] = 'ओ',
	["أ"] = '',
	
	-- diacritics
	[zabar] = "॑",
	[zer] = "" .. hri .. "",
	[pesh] = "" .. hru .. "",
	[jazm] = "" .. halant .. "", 
	[U(0x200C)] = "-", -- ZWNJ (zero-width non-joiner)
	
	-- ligatures
	["ﻻ"] = "ला",
	["ﷲ"] = "अल्लाह",
	
	-- kashida
	["ـ"] = "-", -- kashida, no sound
	

-- numerals
	["١"] = "१", ["٢"] = "२", ["٣"] = "३", ["٤"] = "४", ["٥"] = "५", 
	["٦"] = "६", ["٧"] = "७", ["٨"] = "८", ["٩"] = "९", ["٠"] = "०",
	["۱"] = "१", ["۲"] = "२", ["۳"] = "३", ["۴"] = "४", ["۵"] = "५",
	["۶"] = "६", ["۷"] = "७", ["۸"] = "८", ["۹"] = "९", ["۰"] = "०",
	
	-- punctuation (leave on separate lines)
    ["۔"] = "।", 
	["؟"] = "?", -- question mark
	["،"] = ",", -- comma
	["؛"] = ";", -- semicolon
	["«"] = '“', -- quotation mark
	["»"] = '”', -- quotation mark
	["٪"] = "%", -- percent
	["؉"] = "‰", -- per mille
	["٫"] = ".", -- decimals
	["٬"] = ",", -- thousand
    ["ۓ"] = "-ये", 
	["ۂ"] = "-ए" -- he ye (in ezâfe)
}

local ain = 'ع'
local kzabar = 'ٰ'
local alif = 'ا'
local madda = 'آ'
local ye = 'ی'
local ye2 = 'ئ'
local ye3 = "ے"
local vao = "و"
local ye4 = "ۓ"
local he2 = "ۂ"
local aspirate = 'ھ'
local lam = 'ل'
local noon = 'ن'
local gunDia = '٘'

function export.tr(text, lang, sc)
	
	-- EXCEPTIONS - leave as they are, unless they have been sorted out elsewhere

    text = gsub(text, alif .. noon .. gunDia .. "", "ाँ")
	text = gsub(text, '([' .. consonants .. '])' .. ye .. "ں", "%1ें")
	text = gsub(text, "ؤ" .. pesh, "ऊ")
	text = gsub(text, "وہ", "वह")
	text = gsub(text, alif .. ye2 .. '([' .. zabar .. ']?)' .. '([' .. consonants .. '])', "ाय%2")
	text = gsub(text, madda .. ye2 .. '([' .. zabar .. ']?)' .. '([' .. consonants .. '])', "आय%2")

    -- SSH
    text = gsub(text, "ش"  .. jazm .. ret, "ष्")
    text = gsub(text, "(ش)" .. "([" .. ZZP  .. "])" .. ret, "ष%2")

	-- Tashdeed
    text = gsub(text, '([' .. consonantS2 .. '])' .. tashdid, "%1" .. halant .. "%1")
	text = gsub(text, '([' .. consonantS2 .. '])' .. tashdid .. '([' .. ZZP .. '])', "%1" .. halant .. "%1%2")
	text = gsub(text, '([' .. ZZP .. '])' .. ye .. '([' .. ZZP .. '])' .. tashdid, "%1य्य%2")
	text = gsub(text, '([' .. ZZP .. '])' .. vao .. '([' .. ZZP .. '])' .. tashdid, "%1व्व%2")
	
    -- For some reason the tashdeed gets pushed after the other diacritics, so this line is necessary for tashdeed to work with other diacritics
	text = gsub(text, '([' .. consonants .. '])' .. '([' .. ZZP .. '])' .. tashdid, "%1" .. halant .. "%1%2")	
	
	-- e, instead of i
	--text = gsub(text, jazm .. '([' .. consonants .. '])' .. zer .. '([' .. consonants .. '])', "्%1" .. E .. "%2")
	
	-- tanween diacritic
	text = gsub(text, '([' .. consonants .. '])' .. 'ً' .. alif, "%1न")
	text = gsub(text, alif .. 'ً', "न")
	text = gsub(text, '([' .. consonants .. '])' .. 'ً', "%1न")

	-- khari zabar --
    text = gsub(text, '([' .. consonants .. '])' .. kzabar, "%1" .. LA .. "")
    text = gsub(text, '([' .. vowels .. '])' .. kzabar, "" .. LA .. "")
    text = gsub(text, '([' .. consonants .. '])' .. tashdid .. alif, "%1" .. halant .. "%1" .. LA .. "")
    
    ---- nasalisation
	text = gsub(text, "ن٘([ہو])" , "ँ%1")
	text = gsub(text, "نْ([کگجچٹڈتدن])" , "ं%1")
	text = gsub(text, "ن٘([کگجچٹڈتدن])" , "ं%1")
	text = gsub(text, "مْ([بپمو])" , "ं%1")
	----
	
	-- ‘ain
	text = gsub(text, '([' .. consonants .. '])' .. zabar .. alif .. "", "%1ा")
	text = gsub(text, zer .. ain .. jazm, "े") -- see example: مَواقِع
	
	text = gsub(text, pesh .. ain .. jazm, "ो") -- see example: شُعْلَہ
	text = gsub(text, '([' .. consonants .. '])' .. ain .. zabar .. he, "%1" .. LA .. "")
    text = gsub(text, ain .. alif  .. ain, "आ")
	text = gsub(text, alif .. ain  .. '([' .. consonants .. '])', "" .. E2 .. "%1")
	text = gsub(text, '([' .. consonants .. '])' .. ain .. he, "%1अ")
	text = gsub(text, '([' .. consonants .. '])' .. '([' .. zer .. pesh .. ']?)' .. ain, "%1%2")
	text = gsub(text, ain .. zabar .. vao .. '([' .. consonants .. '])', "औ%1")
	text = gsub(text, ain .. zabar .. ye .. '([' .. consonants .. '])', "ऐ%1")
	text = gsub(text, ain .. zer  .. '([' .. consonants .. '])', "इ%1")
	text = gsub(text, ain .. pesh  .. '([' .. consonants .. '])', "उ%1")
	text = gsub(text, ain .. zer .. ye .. '([' .. consonants .. '])', "ई%1")
    text = gsub(text, ain .. pesh .. vao .. '([' .. consonantS .. '])', "ऊ%1")
    text = gsub(text, ain .. jazm, "" .. LA .. "")
    
    -- medial/final consonants.
	text = gsub(text, zabar .. he .. zer .. ye, "ही")
    text = gsub(text, zer .. he .. alif , "िहा")
	text = gsub(text, zabar .. he .. alif, "हा")
	text = gsub(text, zabar .. he .. '([' .. consonants .. vowels .. '])', "ह%1")
    text = gsub(text, zer .. he .. '([' .. consonants .. vowels .. '])', "िह%1")
	text = gsub(text, '([' .. consonants .. '])' .. alif, "%1ा")
	text = gsub(text, '([' .. consonants .. '])' .. tashdid .. alif, "%1%1ा")
	
	text = gsub(text, '([' .. consonants .. '])' .. vao, "%1ो")
	text = gsub(text, '([' .. consonants .. '])' .. tashdid .. vao, "%1%1ो")
	
	text = gsub(text, zer .. ye .. alif, "िया")
	text = gsub(text, '([' .. consonants .. '])' .. ye .. '([' .. consonants .. '])', "%1े%2")
	text = gsub(text, ye2 .. ye, "ई")
	text = gsub(text, ye2 .. 'ے', "ए")
	text = gsub(text,'([' .. consonants .. '])' .. ye .. ye3, "%1" .. diri .. "ए")
	text = gsub(text, alif .. zabar .. ye3, "" .. AI2 .. "")
	text = gsub(text, '([' .. consonants .. alif .. '])' .. ye2 .. ye, "%1ई")
	text = gsub(text, '([' .. consonants .. '])' .. ye2 .. ye3, "%1ए")
	text = gsub(text, zabar .. ye3, "ै")
	text = gsub(text, '([' .. consonants .. '])' .. zer .. " ", "%1-ए-")
	text = gsub(text, '([' .. consonants .. '])' .. ye3, "%1" .. E .. "")
	text = gsub(text, '([' .. consonants .. '])' .. vao, "%1" .. O .. "")
	text = gsub(text, '([' .. consonants .. '])' .. ye, "%1" .. diri .. "")
	text = gsub(text, alif  .. ye .. '([' .. consonants .. '])', "" .. E2 .. "%1")
	text = gsub(text, alif  .. vao .. '([' .. consonants .. '])', "" .. O2 .. "%1")
    
    -- diacritics
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. vao, "%1ौ")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. ye, "%1ै")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. ye3, "%1" .. AI .. "")
	text = gsub(text, "([" .. consonants .. "])" .. ye .. "", "%1े")
	text = gsub(text, "([" .. consonants  .. "])" .. zer .. ye, "%1" .. diri .. "")
  
	-- Vao
	text = gsub(text, vao .. zer .. ye .. '([' .. consonants .. '])', "वी%1")
	text = gsub(text, vao .. '([' .. ZZP .. '])', "व%1")
	text = gsub(text, '([' .. consonants .. '])' .. zabar .. vao .. alif, "%1वा")
    text = gsub(text, '([' .. consonants .. '])' .. zabar .. vao .. zabar .. alif, "%1वा")
    text = gsub(text, vao .. vao , "वो")
    text = gsub(text, vao .. alif , "वा")

    --VAO alone
    text = gsub(text, " و ", " ओ ")
	
	-- Fatha Majhool --
    text = gsub(text, "([" .. consonants  .. "])" .. zabar .. he .. jazm .. "([" .. ZZP  .. "])" , "%1ह%2")
	
	-- Initial alif
	text = gsub(text, "" .. alif .. '([' .. consonantS .. '])', "अ%1")
	text = gsub(text, alif .. '([' .. consonantS .. '])', "अ%1")
	text = gsub(text, alif .. zabar .. '([' .. consonantS .. '])', "अ%1")
	text = gsub(text, alif .. zabar .. vao .. '([' .. consonants .. '])', "औ%1")
	text = gsub(text, alif  .. vao .. '([' .. consonants .. '])', "ओ%1")
	text = gsub(text, alif  .. ye .. '([' .. consonants .. '])', "ए%1")
	text = gsub(text, alif .. zabar .. ye .. '([' .. consonants .. '])', "ऐ%1")
	text = gsub(text, alif .. pesh .. '([' .. consonantS .. '])', "उ%1")
text = gsub(text, alif .. pesh .. vao .. '([' .. consonantS .. '])', "" .. diru2 .. "%1")
	text = gsub(text, alif .. zer .. '([' .. consonants .. '])', "इ%1")
	text = gsub(text, pesh .. vao, "ू")
	text = gsub(text, alif .. zer .. ye.. '([' .. consonants .. '])', "ई%1")
text = gsub(text, alif  .. ye3, "" .. E2 .. "")

  --- aspirate
	text = gsub(text, "(ک)" .. "([" .. ZZP  .. "])" .. aspirate, "ख%2")
	    text = gsub(text, "(گ)" .. "([" .. ZZP  .. "])" .. aspirate, "घ%2")
	    text = gsub(text, "(چ)" .. "([" .. ZZP  .. "])" .. aspirate, "छ%2")
	    text = gsub(text, "(ج)" .. "([" .. ZZP  .. "])" .. aspirate, "झ%2")
	    text = gsub(text, "(ٹ)" .. "([" .. ZZP  .. "])" .. aspirate, "ठ%2")
	    text = gsub(text, "(ڈ)" .. "([" .. ZZP  .. "])" .. aspirate, "ढ%2")
	    text = gsub(text, "(ت)" .. "([" .. ZZP  .. "])" .. aspirate, "थ%2")
	    text = gsub(text, "(د)" .. "([" .. ZZP  .. "])" .. aspirate, "ध%2")
	    text = gsub(text, "(پ)" .. "([" .. ZZP  .. "])" .. aspirate, "फ%2")
	    text = gsub(text, "(ب)" .. "([" .. ZZP  .. "])" .. aspirate, "भ%2")
	    text = gsub(text, "(ڑ)" .. "([" .. ZZP  .. "])" .. aspirate, "ढ़%2")
	    text = gsub(text, "(م)" .. "([" .. ZZP  .. "])" .. aspirate, "म्ह%2")
	    text = gsub(text, "(ن)" .. "([" .. ZZP  .. "])" .. aspirate, "न्ह%2")
	    text = gsub(text, "(ل)" .. "([" .. ZZP  .. "])" .. aspirate, "ल्ह%2")
	
	-- final he + short vowel disregards the he and transliterates the vowel
	text = gsub(text, "([" .. consonants .. "])" .. he , "%1ह")

	text = gsub(text, zabar .. he .. "([" .. ZZP  .. "])" , "ह%1") -- related to jahez??
	text = gsub(text, '([' .. zabar .. '])' .. he, "ा")
	text = gsub(text, '([' .. zer .. '])' .. he, "ि")
	text = gsub(text, '([' .. zabar .. '])' .. he2 .. " ", "ा-ए-")
	text = gsub(text, zabar .. he .. alif , "हा")
	text = gsub(text, '([' .. zer .. '])' .. he .. alif .. '([' .. consonants .. '])' , "%1हा%2")
	text = gsub(text, he .. alif , "हा")
	text = gsub(text, ye .. he .. jazm , "यह")
	--
    text = gsub(text, "ۂ ", "-ए-")
    text = gsub(text, "ۓ ", "-ये-")
    text = gsub(text, "ࣇ", "ळ")
    text = gsub(text, "شؕ", "ष")
    text = gsub(text, "ڃ", "ञ")
	text = gsub(text, "کھ", "ख")
	text = gsub(text, "گھ", "घ")
	text = gsub(text, "چھ", "छ")
	text = gsub(text, "جھ", "झ")
	text = gsub(text, "ٹھ", "ठ")
	text = gsub(text, "ڈھ", "ढ")
	text = gsub(text, "تھ", "थ")
	text = gsub(text, 'دھ', "ध")
	text = gsub(text, "پھ", "फ")
	text = gsub(text, "بھ", "भ")
    text = gsub(text, "ڑھ", "ढ़")
    text = gsub(text, "مھ", "म्ह")
	text = gsub(text, "نھ", "न्ह")
    text = gsub(text, "لھ", "ल्ह")
	text = gsub(text, "ۂ", "-ए")
    text = gsub(text, "ے", "े")
    
	text = mw.ustring.gsub(text, '.', mapping)
text = mw.ustring.gsub(text, "ललह", "ल्लाह")

	text = mw.ustring.gsub(text, 'ोा', "वा")
	text = mw.ustring.gsub(text, 'ौा', "वा")
	text = mw.ustring.gsub(text, 'ोا', "वा")
	text = mw.ustring.gsub(text, 'व॑ا', "वा")
	text = mw.ustring.gsub(text, 'ɔ̄ا', "वा")
	
	-- Changed these to 'iy(*)', because they will be used for with ی, which are normally written as 'iy'
	text = mw.ustring.gsub(text, 'ी॑ा', "िया")
	text = mw.ustring.gsub(text, 'ी॑', "िय")
	--
	
	text = mw.ustring.gsub(text, 'اे', "ए")
	text = mw.ustring.gsub(text, 'ीا', "िया")
	text = mw.ustring.gsub(text, 'यا', "या")
	
	-- vao as a medial consonant
	text = mw.ustring.gsub(text, "ूू॑", "ुुव")
    text = mw.ustring.gsub(text, "ौ([॑िु])", "व%1")

    -- Final corrections
    text = mw.ustring.gsub(text, "अ॑ا", "आ")
    text = mw.ustring.gsub(text, "ا", "अ") -- to avoid error
        text = mw.ustring.gsub(text, '॑ि', "इ")
        text = mw.ustring.gsub(text, '॑े', "ै")
        text = mw.ustring.gsub(text, '॑ो', "ौ")
        text = mw.ustring.gsub(text, '्यअ', "्या")
	text = mw.ustring.gsub(text, "आ॑", "आ")
    text = mw.ustring.gsub(text, "॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑॑", "आ")
	text = mw.ustring.gsub(text, "āā", "आ")
	text = mw.ustring.gsub(text, "aa", "आ")
	text = mw.ustring.gsub(text, "ë", "ए")
    text = mw.ustring.gsub(text, "ï", "ई")  text = mw.ustring.gsub(text, '॑', "")
text = mw.ustring.gsub(text, "ीा", "िया")
text = mw.ustring.gsub(text, "ाि", "ाइ")
text = mw.ustring.gsub(text, "आि", "आइ")
text = mw.ustring.gsub(text, 'ुो', "ू")
text = mw.ustring.gsub(text, 'िे', "ी")
text = mw.ustring.gsub(text, 'ोا', "वा")
text = mw.ustring.gsub(text, 'ौا', "वा")
text = mw.ustring.gsub(text, 'ौा', "वा")
text = mw.ustring.gsub(text, 'ोा', "वा")
text = mw.ustring.gsub(text, 'अै', "ऐ")
text = mw.ustring.gsub(text, 'ीआ', "िया")
text = mw.ustring.gsub(text, 'ीअ', "िय")
text = mw.ustring.gsub(text, '+', "")
text = mw.ustring.gsub(text, 'اिय', "ई")
text = mw.ustring.gsub(text, 'अै', "ऐ")
text = mw.ustring.gsub(text, 'अा', "आ")
text = mw.ustring.gsub(text, 'अौ', "औ")
text = mw.ustring.gsub(text, 'ी्', "ी")
text = mw.ustring.gsub(text, 'ू्', "ू")
text = mw.ustring.gsub(text, 'े्', "े")
text = mw.ustring.gsub(text, 'ै्', "ै")
text = mw.ustring.gsub(text, 'ो्', "ो")
text = mw.ustring.gsub(text, 'ौ्', "ौ")
	return text
end
return export