모듈:미국 행정구역

이 모듈에 대한 설명문서는 모듈:미국 행정구역/설명문서에서 만들 수 있습니다

local p = {}

local function trim(value)
	if value == nil then
		return ''
	end
	return mw.text.trim(tostring(value))
end

local function getArgs(frame)
	local parent = frame:getParent()
	if parent then
		return parent.args
	end
	return frame.args
end

local function makeLink(target, label)
	return '[[' .. target .. '|' .. label .. ']]'
end

local function safeId(value)
	value = mw.ustring.lower(trim(value))
	if value == '' then
		return 'main'
	end
	value = mw.ustring.gsub(value, '[^%w%-]', '-')
	value = mw.ustring.gsub(value, '%-+', '-')
	value = mw.ustring.gsub(value, '^%-', '')
	value = mw.ustring.gsub(value, '%-$', '')
	return value ~= '' and value or 'main'
end

local function boundedNumber(value, fallback, minimum, maximum)
	local number = tonumber(trim(value)) or fallback
	number = math.floor(number)
	if number < minimum then
		return minimum
	elseif number > maximum then
		return maximum
	end
	return number
end

local regions = {
	northeast = { ko = '북동부', en = 'NORTHEAST', code = 'NE' },
	midwest = { ko = '중서부', en = 'MIDWEST', code = 'MW' },
	south = { ko = '남부', en = 'SOUTH', code = 'SO' },
	west = { ko = '서부', en = 'WEST', code = 'WE' }
}

local states = {
	-- Northeast
	{ code='CT', ko='코네티컷', en='Connecticut', link='코네티컷', capital='하트퍼드', admitted='1788년 1월 9일', order=5, region='northeast', flag='Flag of Connecticut.svg' },
	{ code='ME', ko='메인', en='Maine', link='메인주', capital='오거스타', admitted='1820년 3월 15일', order=23, region='northeast', flag='Flag of Maine.svg' },
	{ code='MA', ko='매사추세츠', en='Massachusetts', link='매사추세츠', capital='보스턴', admitted='1788년 2월 6일', order=6, region='northeast', flag='Flag of Massachusetts.svg' },
	{ code='NH', ko='뉴햄프셔', en='New Hampshire', link='뉴햄프셔', capital='콩코드', admitted='1788년 6월 21일', order=9, region='northeast', flag='Flag of New Hampshire.svg' },
	{ code='RI', ko='로드아일랜드', en='Rhode Island', link='로드아일랜드', capital='프로비던스', admitted='1790년 5월 29일', order=13, region='northeast', flag='Flag of Rhode Island.svg' },
	{ code='VT', ko='버몬트', en='Vermont', link='버몬트', capital='몬트필리어', admitted='1791년 3월 4일', order=14, region='northeast', flag='Flag of Vermont.svg' },
	{ code='NJ', ko='뉴저지', en='New Jersey', link='뉴저지', capital='트렌턴', admitted='1787년 12월 18일', order=3, region='northeast', flag='Flag of New Jersey.svg' },
	{ code='NY', ko='뉴욕', en='New York', link='뉴욕주', capital='올버니', admitted='1788년 7월 26일', order=11, region='northeast', flag='Flag of New York.svg' },
	{ code='PA', ko='펜실베이니아', en='Pennsylvania', link='펜실베이니아', capital='해리스버그', admitted='1787년 12월 12일', order=2, region='northeast', flag='Flag of Pennsylvania.svg' },

	-- Midwest
	{ code='IL', ko='일리노이', en='Illinois', link='일리노이', capital='스프링필드', admitted='1818년 12월 3일', order=21, region='midwest', flag='Flag of Illinois.svg' },
	{ code='IN', ko='인디애나', en='Indiana', link='인디애나', capital='인디애나폴리스', admitted='1816년 12월 11일', order=19, region='midwest', flag='Flag of Indiana.svg' },
	{ code='MI', ko='미시간', en='Michigan', link='미시간', capital='랜싱', admitted='1837년 1월 26일', order=26, region='midwest', flag='Flag of Michigan.svg' },
	{ code='OH', ko='오하이오', en='Ohio', link='오하이오', capital='콜럼버스', admitted='1803년 3월 1일', order=17, region='midwest', flag='Flag of Ohio.svg' },
	{ code='WI', ko='위스콘신', en='Wisconsin', link='위스콘신', capital='매디슨', admitted='1848년 5월 29일', order=30, region='midwest', flag='Flag of Wisconsin.svg' },
	{ code='IA', ko='아이오와', en='Iowa', link='아이오와', capital='디모인', admitted='1846년 12월 28일', order=29, region='midwest', flag='Flag of Iowa.svg' },
	{ code='KS', ko='캔자스', en='Kansas', link='캔자스', capital='토피카', admitted='1861년 1월 29일', order=34, region='midwest', flag='Flag of Kansas.svg' },
	{ code='MN', ko='미네소타', en='Minnesota', link='미네소타', capital='세인트폴', admitted='1858년 5월 11일', order=32, region='midwest', flag='Flag of Minnesota.svg' },
	{ code='MO', ko='미주리', en='Missouri', link='미주리', capital='제퍼슨시티', admitted='1821년 8월 10일', order=24, region='midwest', flag='Flag of Missouri.svg' },
	{ code='NE', ko='네브래스카', en='Nebraska', link='네브래스카', capital='링컨', admitted='1867년 3월 1일', order=37, region='midwest', flag='Flag of Nebraska.svg' },
	{ code='ND', ko='노스다코타', en='North Dakota', link='노스다코타', capital='비즈마크', admitted='1889년 11월 2일', order=39, region='midwest', flag='Flag of North Dakota.svg' },
	{ code='SD', ko='사우스다코타', en='South Dakota', link='사우스다코타', capital='피어', admitted='1889년 11월 2일', order=40, region='midwest', flag='Flag of South Dakota.svg' },

	-- South
	{ code='DE', ko='델라웨어', en='Delaware', link='델라웨어', capital='도버', admitted='1787년 12월 7일', order=1, region='south', flag='Flag of Delaware.svg' },
	{ code='FL', ko='플로리다', en='Florida', link='플로리다', capital='탤러해시', admitted='1845년 3월 3일', order=27, region='south', flag='Flag of Florida.svg' },
	{ code='GA', ko='조지아', en='Georgia', link='조지아주', capital='애틀랜타', admitted='1788년 1월 2일', order=4, region='south', flag='Flag of Georgia (U.S. state).svg' },
	{ code='MD', ko='메릴랜드', en='Maryland', link='메릴랜드', capital='애나폴리스', admitted='1788년 4월 28일', order=7, region='south', flag='Flag of Maryland.svg' },
	{ code='NC', ko='노스캐롤라이나', en='North Carolina', link='노스캐롤라이나', capital='롤리', admitted='1789년 11월 21일', order=12, region='south', flag='Flag of North Carolina.svg' },
	{ code='SC', ko='사우스캐롤라이나', en='South Carolina', link='사우스캐롤라이나', capital='컬럼비아', admitted='1788년 5월 23일', order=8, region='south', flag='Flag of South Carolina.svg' },
	{ code='VA', ko='버지니아', en='Virginia', link='버지니아', capital='리치먼드', admitted='1788년 6월 25일', order=10, region='south', flag='Flag of Virginia.svg' },
	{ code='WV', ko='웨스트버지니아', en='West Virginia', link='웨스트버지니아', capital='찰스턴', admitted='1863년 6월 20일', order=35, region='south', flag='Flag of West Virginia.svg' },
	{ code='AL', ko='앨라배마', en='Alabama', link='앨라배마', capital='몽고메리', admitted='1819년 12월 14일', order=22, region='south', flag='Flag of Alabama.svg' },
	{ code='KY', ko='켄터키', en='Kentucky', link='켄터키', capital='프랭크퍼트', admitted='1792년 6월 1일', order=15, region='south', flag='Flag of Kentucky.svg' },
	{ code='MS', ko='미시시피', en='Mississippi', link='미시시피주', capital='잭슨', admitted='1817년 12월 10일', order=20, region='south', flag='Flag of Mississippi.svg' },
	{ code='TN', ko='테네시', en='Tennessee', link='테네시', capital='내슈빌', admitted='1796년 6월 1일', order=16, region='south', flag='Flag of Tennessee.svg' },
	{ code='AR', ko='아칸소', en='Arkansas', link='아칸소', capital='리틀록', admitted='1836년 6월 15일', order=25, region='south', flag='Flag of Arkansas.svg' },
	{ code='LA', ko='루이지애나', en='Louisiana', link='루이지애나', capital='배턴루지', admitted='1812년 4월 30일', order=18, region='south', flag='Flag of Louisiana.svg' },
	{ code='OK', ko='오클라호마', en='Oklahoma', link='오클라호마', capital='오클라호마시티', admitted='1907년 11월 16일', order=46, region='south', flag='Flag of Oklahoma.svg' },
	{ code='TX', ko='텍사스', en='Texas', link='텍사스', capital='오스틴', admitted='1845년 12월 29일', order=28, region='south', flag='Flag of Texas.svg' },

	-- West
	{ code='AZ', ko='애리조나', en='Arizona', link='애리조나', capital='피닉스', admitted='1912년 2월 14일', order=48, region='west', flag='Flag of Arizona.svg' },
	{ code='CO', ko='콜로라도', en='Colorado', link='콜로라도', capital='덴버', admitted='1876년 8월 1일', order=38, region='west', flag='Flag of Colorado.svg' },
	{ code='ID', ko='아이다호', en='Idaho', link='아이다호', capital='보이시', admitted='1890년 7월 3일', order=43, region='west', flag='Flag of Idaho.svg' },
	{ code='MT', ko='몬태나', en='Montana', link='몬태나', capital='헬레나', admitted='1889년 11월 8일', order=41, region='west', flag='Flag of Montana.svg' },
	{ code='NV', ko='네바다', en='Nevada', link='네바다', capital='카슨시티', admitted='1864년 10월 31일', order=36, region='west', flag='Flag of Nevada.svg' },
	{ code='NM', ko='뉴멕시코', en='New Mexico', link='뉴멕시코', capital='샌타페이', admitted='1912년 1월 6일', order=47, region='west', flag='Flag of New Mexico.svg' },
	{ code='UT', ko='유타', en='Utah', link='유타', capital='솔트레이크시티', admitted='1896년 1월 4일', order=45, region='west', flag='Flag of Utah.svg' },
	{ code='WY', ko='와이오밍', en='Wyoming', link='와이오밍', capital='샤이엔', admitted='1890년 7월 10일', order=44, region='west', flag='Flag of Wyoming.svg' },
	{ code='AK', ko='알래스카', en='Alaska', link='알래스카', capital='주노', admitted='1959년 1월 3일', order=49, region='west', flag='Flag of Alaska.svg' },
	{ code='CA', ko='캘리포니아', en='California', link='캘리포니아', capital='새크라멘토', admitted='1850년 9월 9일', order=31, region='west', flag='Flag of California.svg' },
	{ code='HI', ko='하와이', en='Hawaii', link='하와이', capital='호놀룰루', admitted='1959년 8월 21일', order=50, region='west', flag='Flag of Hawaii.svg' },
	{ code='OR', ko='오리건', en='Oregon', link='오리건', capital='세일럼', admitted='1859년 2월 14일', order=33, region='west', flag='Flag of Oregon.svg' },
	{ code='WA', ko='워싱턴', en='Washington', link='워싱턴주', capital='올림피아', admitted='1889년 11월 11일', order=42, region='west', flag='Flag of Washington.svg' }
}

local mapOrder = {'WA','OR','CA','ID','NV','AZ','MT','WY','UT','CO','NM','ND','SD','NE','KS','OK','TX','MN','IA','MO','AR','LA','WI','MI','IL','IN','OH','KY','TN','MS','AL','GA','FL','SC','NC','VA','WV','PA','NY','VT','NH','ME','MA','CT','RI','NJ','MD','DE','AK','HI'}
local mapHitOrder = {'DE','RI','CT','HI','NJ','MA','NH','VT','MD','WV','SC','ME','IN','VA','KY','OH','TN','PA','LA','MS','NY','NC','AL','AR','WI','MI','IA','IL','FL','GA','WA','OK','ND','MO','SD','NE','KS','ID','MN','UT','OR','WY','AK','CO','NV','AZ','NM','MT','CA','TX'}

local mapAreas = {
	WA = {
		'poly 237 126 234 159 201 153 201 150 168 150 168 147 153 150 153 147 138 147 129 141 111 144 105 138 105 123 84 111 84 105 90 102 87 99 87 90 90 90 87 87 87 57 84 57 87 45 93 45 99 54 114 57 120 63 123 60 135 63 135 42 132 39 138 36 138 39 174 45 183 51 252 66',
	},
	OR = {
		'poly 138 144 138 147 153 147 153 150 165 150 165 147 168 150 201 150 201 153 234 159 234 165 240 168 240 177 231 183 225 198 216 204 219 225 213 234 204 282 189 282 189 279 177 279 177 276 93 258 93 255 84 255 75 249 42 243 36 237 39 210 45 207 45 201 57 189 57 180 66 168 69 150 72 150 78 129 81 129 81 120 87 114 93 114 105 123 105 138 111 144 123 144 123 141',
	},
	CA = {
		'poly 126 384 132 390 132 396 138 399 138 405 144 408 144 414 150 417 150 423 156 426 156 432 165 438 165 444 177 456 177 462 183 465 183 471 189 474 192 486 198 489 198 495 204 498 210 513 216 516 216 531 219 531 222 543 210 549 210 561 204 564 204 573 201 573 207 579 204 588 135 579 132 576 132 555 120 543 120 537 114 537 108 525 96 522 84 504 60 498 63 474 54 462 48 438 42 435 42 423 48 423 51 414 42 411 42 405 39 405 39 384 42 381 36 378 30 348 21 339 24 324 27 324 27 303 21 297 21 285 27 282 27 276 39 258 39 240 132 267 132 282 129 282 123 318 120 318 114 351 111 351 111 363',
	},
	ID = {
		'poly 276 300 261 297 261 294 204 285 204 270 207 270 213 234 219 225 216 204 225 198 231 183 240 177 240 168 234 165 234 135 237 135 240 108 243 108 252 66 270 69 270 87 267 87 270 123 282 135 282 144 294 153 282 183 285 186 297 183 297 195 303 201 303 210 309 213 309 225 342 228 348 222 354 231 342 309',
	},
	NV = {
		'poly 267 318 237 483 231 486 228 480 219 480 219 492 216 492 219 510 213 516 210 507 204 504 204 498 198 495 198 489 192 486 189 474 183 471 183 465 177 462 177 456 171 453 165 438 156 432 156 426 150 423 150 417 144 414 144 408 138 405 138 399 132 396 132 390 126 387 126 381 120 378 120 372 111 363 117 327 120 327 129 282 132 282 132 267 189 279 189 282 204 282 204 285 216 285 216 288 270 297',
	},
	AZ = {
		'poly 366 480 342 654 285 648 282 642 270 639 267 633 249 627 246 621 234 618 231 612 225 612 216 603 195 594 198 588 207 585 207 579 201 573 204 573 204 564 210 561 210 549 222 543 222 537 216 531 219 480 228 480 234 486 243 456 285 462 285 465 303 465 303 468 360 474',
	},
	MT = {
		'poly 498 108 522 108 525 111 513 234 435 228 435 225 414 225 414 222 393 222 393 219 372 219 372 216 357 216 354 231 351 231 348 222 342 228 309 225 309 213 303 210 303 201 297 195 297 183 291 183 291 186 282 183 294 153 282 144 282 135 270 123 267 87 270 87 270 72 285 72 285 75 300 75 300 78 330 81 330 84 345 84 345 87 423 96 423 99 447 99 447 102 471 102 471 105 498 105',
	},
	WY = {
		'poly 411 354 336 342 339 315 342 315 339 309 342 309 348 261 351 261 354 219 372 216 372 219 393 219 393 222 414 222 414 225 435 225 435 228 510 234 513 252 510 252 510 300 507 300 507 330 504 330 504 363',
	},
	UT = {
		'poly 381 363 366 477 243 456 243 438 246 438 246 423 249 423 249 408 252 408 252 393 255 393 255 375 258 375 270 300 339 309 342 315 339 315 336 342 339 345 381 348 384 351',
	},
	CO = {
		'poly 429 354 462 357 462 360 549 366 543 495 435 486 435 483 387 480 387 477 366 474 375 399 378 399 378 378 381 378 381 354 384 351',
	},
	NM = {
		'poly 501 492 519 492 516 495 519 510 516 510 516 522 513 522 513 564 510 564 504 651 438 648 438 645 408 645 408 648 363 645 363 657 345 657 342 642 345 642 345 621 348 621 348 600 351 600 351 576 354 576 354 552 357 552 366 474 387 477 387 480',
	},
	ND = {
		'poly 672 126 669 126 669 144 675 153 681 213 576 210 576 207 516 204 522 117 525 117 522 108 528 108 528 111 573 111 573 114 666 114 672 120',
	},
	SD = {
		'poly 537 207 681 213 678 228 684 228 684 291 681 291 681 297 684 297 681 318 684 321 678 321 678 315 672 315 666 309 645 312 639 306 615 306 615 303 510 300 510 252 513 252 510 234 513 234 516 204',
	},
	NE = {
		'poly 690 336 690 345 693 345 696 363 699 363 699 381 702 381 708 402 549 399 549 366 504 363 507 300 615 303 615 306 639 306 645 312 666 309 666 312 678 315 678 321 687 324 687 336',
	},
	KS = {
		'poly 720 423 729 429 729 501 726 498 543 495 549 399 714 402 714 405 720 405 720 414 717 414',
	},
	OK = {
		'poly 633 498 726 498 729 501 732 543 735 543 735 603 723 603 720 597 714 597 714 600 699 597 690 603 669 597 660 603 657 597 645 600 642 591 627 591 627 588 615 588 612 582 597 582 591 576 594 513 528 510 528 507 519 510 516 507 519 492',
	},
	TX = {
		'poly 492 744 480 741 477 735 471 735 459 723 456 699 453 699 450 687 441 684 426 669 426 663 420 663 417 654 408 651 408 645 504 651 516 510 528 507 528 510 594 513 591 576 597 582 612 582 615 588 642 591 645 600 657 597 660 603 669 597 681 600 681 603 690 603 699 597 711 597 711 600 720 597 723 603 732 603 735 609 747 609 747 657 756 666 753 672 762 681 756 732 738 735 714 759 708 759 705 765 681 774 675 783 654 792 657 807 654 807 651 828 657 837 654 840 657 852 651 852 645 846 630 846 630 843 615 840 603 831 603 819 597 816 597 798 573 774 567 750 549 732 549 726 516 720 504 729 504 738 495 747',
	},
	MN = {
		'poly 675 171 675 153 669 144 669 126 672 126 669 117 711 117 714 105 720 105 720 120 726 126 741 126 741 129 762 126 765 132 780 135 789 144 798 144 801 138 825 141 825 144 831 141 831 147 822 147 822 150 810 153 807 159 801 159 801 165 777 189 777 210 768 213 768 225 771 225 768 249 777 252 780 258 786 258 789 267 804 273 804 288 687 291 684 288 684 228 678 228 681 195 678 195 678 174',
	},
	IA = {
		'poly 720 384 699 381 696 351 690 345 687 324 681 318 681 309 684 309 681 291 684 288 687 291 807 288 810 312 819 312 825 327 831 330 828 351 813 354 813 363 816 363 813 378 807 384 801 384 801 381',
	},
	MO = {
		'poly 702 387 702 384 801 381 807 408 825 423 825 432 840 435 837 450 834 450 837 459 843 465 855 468 861 492 870 492 864 513 858 516 861 519 858 528 840 525 846 519 843 510 732 513 729 516 729 429 717 420 720 405 708 402',
	},
	AR = {
		'poly 801 624 747 627 747 609 732 606 735 603 732 513 843 510 846 519 840 525 858 528 858 534 849 540 849 558 840 564 837 579 825 591 825 603 822 603 825 624',
	},
	LA = {
		'poly 792 624 822 624 822 630 828 636 828 648 825 648 828 657 822 660 822 666 816 672 816 687 864 687 864 699 873 708 867 714 867 723 873 717 879 717 879 723 870 729 870 735 876 735 876 738 882 735 888 741 885 747 870 741 867 735 861 735 861 744 849 741 840 750 840 747 831 747 828 738 819 735 819 729 807 729 807 726 801 729 801 735 789 735 789 732 777 732 777 729 756 732 762 681 753 672 756 666 747 657 747 627',
	},
	WI = {
		'poly 810 294 804 288 804 273 789 267 786 258 780 258 777 252 768 249 768 231 771 231 768 213 777 210 777 189 801 183 804 177 810 177 807 189 822 189 831 198 870 204 879 213 876 222 882 225 876 249 894 231 894 237 888 243 885 282 882 282 882 294 885 294 885 306 888 306 885 315 822 318 819 312 810 312 810 303 807 303',
	},
	MI = {
		'poly 891 219 882 231 882 225 876 222 879 213 870 204 855 204 855 201 831 198 822 192 822 186 849 174 864 156 870 156 870 162 864 165 861 177 876 174 882 186 891 186 891 183 903 186 909 177 927 177 933 171 936 171 939 183 957 180 957 195 915 198 903 207 894 207',
		'poly 1005 270 1008 297 1002 297 1002 306 996 309 993 327 963 330 963 333 960 330 918 333 921 318 924 318 924 294 915 282 915 255 918 255 921 231 936 234 936 219 942 216 939 213 942 204 957 204 966 213 975 213 978 225 984 228 984 243 978 246 972 267 981 267 984 258 990 252 996 252 1002 258',
	},
	IL = {
		'poly 804 396 804 384 816 372 813 354 828 351 828 342 831 342 831 330 825 327 822 318 852 318 852 315 885 315 888 312 888 327 894 330 894 339 897 339 903 441 894 450 897 462 891 468 891 477 885 477 885 489 870 486 867 492 861 492 858 471 843 465 834 453 840 435 825 432 825 423 807 408',
	},
	IN = {
		'poly 903 432 897 339 909 339 915 333 960 330 969 426 957 429 957 435 951 438 945 453 933 450 930 459 921 456 918 462 915 459 900 459 900 462 894 459 894 450 900 447',
	},
	OH = {
		'poly 1059 309 1065 354 1068 354 1065 357 1068 360 1065 378 1062 378 1062 387 1044 399 1044 408 1035 411 1032 429 1026 432 1017 420 1011 426 1008 423 996 426 996 423 987 423 981 414 969 414 960 333 996 327 996 330 1008 330 1011 336 1017 336 1023 330 1035 330 1041 324 1041 318 1047 318 1050 312',
	},
	KY = {
		'poly 1008 426 1017 420 1026 429 1026 438 1032 441 1032 447 1044 456 1044 462 1035 465 1032 474 1020 486 1014 486 1011 492 996 492 996 495 984 492 984 495 963 495 963 498 960 495 918 498 918 501 900 501 897 507 867 507 864 510 864 504 870 498 867 489 870 486 885 489 885 477 891 477 894 462 900 462 900 459 918 462 921 456 930 459 933 450 945 453 945 447 951 444 957 429 969 426 969 414 981 414 987 423 996 423 996 426 1008 423',
	},
	TN = {
		'poly 867 507 897 507 900 501 918 501 918 498 951 498 951 495 963 498 963 495 984 495 984 492 996 495 996 492 1035 489 1035 486 1053 486 1053 483 1059 486 1059 492 1050 501 1032 507 1029 516 1023 516 1020 522 1005 525 1005 534 999 537 996 546 978 546 978 549 969 546 969 549 948 549 948 552 849 558 849 540 858 534 858 522 861 522 858 516',
	},
	MS = {
		'poly 900 660 903 705 888 702 888 705 870 708 870 702 864 699 864 687 816 687 816 672 819 672 822 660 828 657 825 654 828 636 822 630 822 624 825 624 822 603 825 603 828 585 837 579 837 570 846 558 900 555 900 630 897 630',
	},
	AL = {
		'poly 930 690 927 705 921 705 918 693 909 702 903 702 903 678 900 678 900 654 897 654 900 555 966 549 966 561 972 567 981 612 993 630 993 636 990 636 993 675 924 681 924 687',
	},
	GA = {
		'poly 1053 681 999 681 993 675 993 666 990 666 993 663 993 657 990 657 993 630 984 621 981 600 978 600 975 579 972 579 972 567 966 561 969 546 978 549 978 546 996 546 996 543 1026 543 1026 552 1038 555 1041 564 1047 567 1047 573 1059 576 1068 585 1068 591 1080 597 1080 603 1092 615 1092 621 1101 624 1101 630 1095 636 1095 651 1092 651 1092 672 1077 669 1077 684 1071 678',
	},
	FL = {
		'poly 960 699 930 702 930 690 924 687 924 681 996 675 999 681 1071 678 1077 684 1077 669 1089 669 1095 675 1095 684 1098 684 1098 693 1101 693 1107 711 1113 714 1119 729 1128 732 1128 750 1137 756 1146 780 1152 783 1152 813 1155 813 1155 819 1149 822 1152 834 1146 840 1128 843 1125 828 1119 822 1107 822 1101 804 1092 807 1089 795 1074 780 1080 762 1074 768 1068 765 1071 735 1065 732 1062 723 1053 723 1035 702 1017 699 1017 705 1011 705 1005 714 990 714 984 705 978 705 972 699',
	},
	SC = {
		'poly 1059 576 1047 573 1047 567 1041 564 1038 555 1026 552 1026 540 1035 540 1047 531 1086 528 1092 537 1122 534 1125 540 1140 546 1146 555 1152 555 1140 567 1140 585 1131 588 1131 594 1110 612 1110 618 1104 624 1095 624 1092 615 1083 609 1080 597 1071 594',
	},
	NC = {
		'poly 1200 459 1206 462 1206 471 1215 477 1209 495 1203 501 1197 501 1197 507 1194 507 1194 513 1203 513 1203 519 1176 531 1170 546 1167 546 1167 555 1146 555 1140 546 1134 546 1122 534 1092 537 1086 528 1047 531 1047 534 1026 540 1026 543 999 543 999 537 1005 534 1005 525 1020 522 1023 516 1029 516 1032 507 1050 501 1059 492 1056 483 1107 480 1107 477 1143 474 1143 471 1158 471 1158 468 1185 465 1185 462',
	},
	VA = {
		'poly 1191 450 1197 450 1203 459 1158 468 1158 471 1107 477 1107 480 1014 489 1044 459 1050 465 1059 465 1059 462 1068 462 1068 459 1086 453 1089 435 1098 423 1098 414 1104 414 1104 417 1110 414 1110 405 1122 396 1125 381 1131 381 1131 384 1146 381 1146 387 1161 393 1161 399 1158 399 1161 411 1173 411 1173 414 1185 417 1185 432 1188 432 1185 441',
	},
	WV = {
		'poly 1095 375 1134 372 1140 378 1140 384 1125 381 1125 390 1110 405 1110 414 1107 417 1098 414 1098 423 1089 435 1086 453 1080 453 1068 462 1050 465 1026 438 1026 432 1035 423 1035 411 1044 408 1044 399 1062 387 1065 363 1068 363 1071 381 1074 378 1095 378',
	},
	PA = {
		'poly 1137 369 1071 381 1059 309 1071 300 1077 300 1077 306 1083 306 1083 303 1098 303 1098 300 1113 300 1113 297 1128 297 1128 294 1143 294 1143 291 1158 291 1158 288 1173 288 1173 285 1185 288 1188 297 1197 303 1194 315 1191 315 1194 330 1206 342 1200 345 1197 354 1188 354 1185 360',
	},
	NY = {
		'poly 1230 321 1218 321 1221 309 1194 303 1188 297 1188 291 1179 285 1143 291 1143 294 1128 294 1128 297 1113 297 1113 300 1098 300 1098 303 1083 303 1083 306 1077 306 1074 297 1092 279 1092 270 1086 267 1086 261 1101 258 1101 255 1122 258 1128 252 1137 252 1143 243 1149 243 1149 234 1146 234 1149 228 1143 225 1143 219 1152 213 1152 207 1158 204 1158 198 1167 189 1203 183 1206 210 1212 216 1212 225 1215 225 1218 243 1221 243 1224 312 1248 306 1251 300 1257 300 1248 312 1242 312',
	},
	VT = {
		'poly 1215 231 1212 216 1206 210 1203 180 1215 180 1215 177 1227 177 1227 174 1242 171 1245 192 1236 195 1236 249 1221 249 1218 231',
	},
	NH = {
		'poly 1245 192 1242 165 1251 162 1254 177 1257 177 1257 189 1260 189 1260 198 1266 207 1266 219 1272 219 1275 234 1266 237 1263 243 1236 246 1236 195',
	},
	ME = {
		'poly 1326 138 1329 141 1338 138 1341 153 1326 168 1320 168 1320 174 1302 174 1302 189 1296 189 1293 198 1281 201 1278 225 1272 225 1272 219 1266 219 1266 207 1263 207 1257 177 1251 168 1251 159 1257 159 1257 147 1263 141 1260 120 1263 120 1263 102 1266 102 1269 87 1275 84 1281 90 1287 81 1299 81 1305 87 1314 126 1326 129',
	},
	MA = {
		'poly 1230 273 1221 276 1221 249 1263 243 1272 234 1278 237 1275 252 1284 252 1287 261 1296 267 1293 273 1284 270 1284 276 1278 276 1272 285 1263 285 1260 267',
		'poly 1302 261 1305 261 1305 267 1296 267',
	},
	CT = {
		'poly 1251 267 1263 270 1263 288 1257 288 1251 294 1239 294 1230 306 1224 306 1224 279 1221 276',
	},
	RI = {
		'poly 1269 258 1278 260 1280 274 1272 278 1268 267',
	},
	NJ = {
		'poly 1200 333 1194 330 1194 318 1191 318 1194 306 1203 303 1203 306 1221 309 1218 327 1224 336 1224 354 1215 366 1215 375 1209 375 1191 363 1191 354 1197 354 1200 345 1206 342',
	},
	MD = {
		'poly 1209 408 1200 414 1194 408 1185 408 1182 387 1179 387 1182 381 1176 372 1173 375 1173 402 1179 405 1179 411 1176 414 1161 411 1158 408 1161 393 1146 387 1146 381 1140 381 1134 375 1134 369 1185 360 1191 354 1188 366 1194 366 1200 384 1209 387',
	},
	DE = {
		'poly 1203 417 1203 435 1197 441 1194 438 1197 420 1200 420 1200 411 1209 408 1209 414',
	},
	AK = {
		'poly 231 813 228 816 219 810 204 810 204 807 192 804 192 795 186 795 186 798 180 798 183 810 171 807 168 813 156 816 159 798 171 795 171 792 159 792 150 801 150 807 144 807 138 813 141 822 135 828 123 831 120 837 102 840 99 849 87 846 87 849 72 849 63 855 57 855 57 852 48 855 48 849 66 849 69 843 96 840 114 822 114 813 102 813 99 807 87 807 90 789 75 786 72 759 78 753 84 753 87 744 93 744 96 750 114 744 114 729 102 732 102 729 87 726 84 705 105 702 108 705 105 711 117 714 117 699 111 696 105 675 108 672 120 675 129 660 153 660 156 654 162 654 168 663 177 663 177 669 183 669 183 672 189 669 195 675 225 681 225 807 240 810 249 819 249 825 243 825',
		'poly 51 726 51 723 54 723 54 726',
		'poly 60 729 63 732 57 732 57 726',
		'poly 60 774 60 777 66 777 66 783 57 780 57 774',
		'poly 264 846 249 825 255 825 258 816 267 816 270 825 282 831 291 852 306 855 309 873 303 873 300 867 294 867 297 876 285 873 282 861 270 858 270 852',
		'poly 147 828 147 840 132 846 129 837 135 831 141 831 144 825 150 825',
		'poly 30 855 33 858 27 858',
	},
	HI = {
		'poly 357 774 351 774 354 765 363 768 363 777 357 777',
		'poly 396 789 399 783 408 783 411 795 399 795',
		'poly 435 801 432 801 432 798 435 798',
		'poly 441 801 438 801 438 798 441 798',
		'poly 447 810 444 810 444 804 453 804 462 810 462 816 453 819',
		'poly 432 810 432 807 438 807 438 813',
		'poly 480 831 483 837 492 840 498 855 483 858 480 867 471 870 468 849 462 843 471 837 468 828',
	},
}

local stateByCode = {}
for _, state in ipairs(states) do
	stateByCode[state.code] = state
end

local function getOverride(args, state, suffix)
	local byCode = trim(args[state.code .. suffix])
	if byCode ~= '' then
		return byCode
	end
	return trim(args[state.ko .. suffix])
end

local function stateTarget(args, state)
	local override = getOverride(args, state, '문서')
	if override ~= '' then
		return override
	end
	local prefix = trim(args['문서접두어'])
	if prefix ~= '' then
		return prefix .. state.ko
	end
	return state.link
end

local function stateFlag(args, state)
	local override = getOverride(args, state, '국기')
	return override ~= '' and override or state.flag
end

local function stateDescription(args, state)
	local override = getOverride(args, state, '설명')
	if override ~= '' then
		return override
	end
	local region = regions[state.region]
	return '미국 ' .. region.ko .. '에 위치한 주이다. '
		.. '주도는 ' .. state.capital .. '이며, '
		.. state.admitted .. ' 제' .. state.order .. '번째로 연방에 가입했다.'
end

local function renderHeader(root)
	local header = root:tag('div'):addClass('usmap2-header')
	header:tag('span'):addClass('usmap2-stripes usmap2-stripes-left')
	header:tag('span'):addClass('usmap2-stripes usmap2-stripes-right')
	header:tag('div'):addClass('usmap2-title')
		:wikitext('미합중국 연방 행정구역')
	header:tag('div'):addClass('usmap2-subtitle')
		:wikitext('EXECUTIVE DIRECTORY OF THE UNITED STATES')
end

local function normalizeMapFile(value)
	local mapFile = trim(value)
	if mapFile == '' then
		return 'US Federal States.png'
	end

	-- ImageMap 본문에는 파일 이름만 넘긴다. 사용자가 네임스페이스까지
	-- 적었을 때 네임스페이스 접두어가 중복되는 상황을 방지한다.
	mapFile = mw.ustring.gsub(mapFile, '^%s*[Ff][Ii][Ll][Ee]%s*:%s*', '')
	mapFile = mw.ustring.gsub(mapFile, '^%s*[Ii][Mm][Aa][Gg][Ee]%s*:%s*', '')
	mapFile = mw.ustring.gsub(mapFile, '^%s*파일%s*:%s*', '')
	mapFile = trim(mapFile)

	if mapFile == '' then
		return 'US Federal States.png'
	end
	return mapFile
end

local function imageMapSource(args, prefix, width)
	local mapFile = normalizeMapFile(args['지도'])

	local lines = {
		-- 위키스의 구형 ImageMap 사용례에 맞춰 legacy Image: 접두어를 쓴다.
		-- 첫 줄의 alt 인수도 제거해 확장 기능의 이미지 파싱 실패를 피한다.
		'Image:' .. mapFile .. '|' .. width .. 'px'
	}

	for _, code in ipairs(mapHitOrder) do
		local state = stateByCode[code]
		local target = '#' .. prefix .. '-panel-' .. mw.ustring.lower(code)
		for _, geometry in ipairs(mapAreas[code]) do
			lines[#lines + 1] = geometry .. ' [[' .. target
				.. '|' .. state.ko .. ' 선택]]'
		end
	end

	lines[#lines + 1] = 'desc none'
	return table.concat(lines, '\n')
end

local function renderImageMap(frame, parent, args, prefix, width, className)
	local wrapper = parent:tag('div'):addClass(className)
	wrapper:wikitext(frame:extensionTag(
		'imagemap',
		imageMapSource(args, prefix, width)
	))
end

local function renderMap(frame, parent, args, prefix)
	local desktopWidth = boundedNumber(args['데스크톱너비'], 900, 500, 1200)
	local mobileWidth = boundedNumber(args['모바일너비'], 480, 320, 700)
	local mapFrame = parent:tag('div'):addClass('usmap2-map-frame')

	mapFrame:tag('div'):addClass('usmap2-map-heading')
		:wikitext('UNITED STATES FEDERAL DIVISION DIRECTORY')

	local scroll = mapFrame:tag('div'):addClass('usmap2-map-scroll')
	renderImageMap(frame, scroll, args, prefix, desktopWidth, 'usmap2-map-desktop')
	renderImageMap(frame, scroll, args, prefix, mobileWidth, 'usmap2-map-mobile')

	mapFrame:tag('div'):addClass('usmap2-map-caption')
		:wikitext('ARCHIVAL CODIFICATION INDEX · FIFTY FEDERAL STATES')
end

local function addMeta(parent, label, value)
	local item = parent:tag('div'):addClass('usmap2-meta-item')
	item:tag('span'):addClass('usmap2-meta-key'):wikitext(label)
	item:tag('span'):addClass('usmap2-meta-value'):wikitext(value)
end

local function renderStatePanel(parent, args, prefix, state)
	local code = mw.ustring.lower(state.code)
	local region = regions[state.region]
	local target = stateTarget(args, state)
	local panel = parent:tag('div')
		:addClass('usmap2-panel')
		:attr('id', prefix .. '-panel-' .. code)

	panel:tag('div')
		:addClass('usmap2-dimmer')
		:wikitext(makeLink('#' .. prefix .. '-close', '&#8203;'))

	local card = panel:tag('div'):addClass('usmap2-card')
	card:tag('div'):addClass('usmap2-card-line')

	local flag = card:tag('div'):addClass('usmap2-flag')
	flag:wikitext('[[파일:' .. stateFlag(args, state)
		.. '|border|180px|link=' .. target .. '|alt=' .. state.ko .. ' 주기]]')

	local content = card:tag('div'):addClass('usmap2-card-content')
	content:tag('div'):addClass('usmap2-kicker')
		:wikitext('UNITED STATES FEDERAL RECORD · ' .. state.code .. ' · ' .. region.en)
	content:tag('div'):addClass('usmap2-name-ko')
		:wikitext(makeLink(target, state.ko))
	content:tag('div'):addClass('usmap2-name-en')
		:wikitext(mw.ustring.upper(state.en))
	content:tag('div'):addClass('usmap2-description')
		:wikitext(stateDescription(args, state))

	local meta = content:tag('div'):addClass('usmap2-meta')
	addMeta(meta, 'CAPITAL', state.capital)
	addMeta(meta, 'ADMITTED', state.admitted)
	addMeta(meta, 'ORDER', string.format('%02d / 50', state.order))
	addMeta(meta, 'CODE', state.code)

	content:tag('div'):addClass('usmap2-open')
		:wikitext(makeLink(target, '주 문서 열람  →'))
	content:tag('div'):addClass('usmap2-close')
		:wikitext(makeLink('#' .. prefix .. '-close', '기록 닫기'))
end

function p.main(frame)
	local args = getArgs(frame)
	local prefix = 'usmap2-' .. safeId(args.id)
	local root = mw.html.create('div')
		:addClass('usmap2-root')
		:addClass('mw-collapsible')
		:addClass('mw-collapsed')
		:attr('id', prefix)
		:attr('data-expandtext', '★ [ 펼치기 · 접기 ] ★')
		:attr('data-collapsetext', '★ [ 펼치기 · 접기 ] ★')
		:attr('data-usmap-version', 'archival-imagemap-v4.1.4')

	root:tag('span'):addClass('usmap2-close-target')
		:attr('id', prefix .. '-close')
	renderHeader(root)
	root:tag('div'):addClass('usmap2-toggle-bed')

	local body = root:tag('div'):addClass('mw-collapsible-content')
	renderMap(frame, body, args, prefix)
	body:tag('div'):addClass('usmap2-instruction')
		:wikitext('주를 선택하면 해당 연방 기록 카드가 열립니다.')

	local panels = root:tag('div'):addClass('usmap2-panels')
	for _, code in ipairs(mapOrder) do
		renderStatePanel(panels, args, prefix, stateByCode[code])
	end

	return tostring(root)
end

function p.version()
	return '미국 행정구역 v4.1.4 FILENAME FIX / Lua'
end

p.render = p.main
return p