<!-- Student Merit Store - localized strings for the plugin's JavaScript.

     .js files are NOT template-processed by PowerSchool, so they cannot contain [text:] keys. This
     wildcard is a .txt (which IS processed), so the keys resolve here and the scripts read plain
     strings off meritText. Include it with [wc:meritStoreText] BEFORE any merit script tag on any
     page that loads one.

     Defined in one place rather than inline per page so the pages that share a script cannot drift
     apart. Every value maps to a nicktech.merit.js.* key in MessageKeys.

     Values are whole sentences with {0}/{1} placeholders rather than fragments the scripts glue
     together, so a translator can move the numbers around inside the sentence.

     NOTE: tags are named without their leading ~ in this comment, since tags render even inside
     HTML comments. -->

<!-- The four values below are built from DISTRICT-ENTERED text (the Naming settings), so they are
     emitted as the CONTENT of non-executing script blocks rather than straight into a JavaScript
     string literal. A quote or an apostrophe in a configured name is then just text; inside a
     literal it would terminate the string and break every merit page at once.

     type="text/plain" is never executed and IS valid inside head, which matters because most pages
     include this wildcard right next to [wc:commonscripts]. Read back with textContent below. -->
<script type="text/plain" id="meritUnitSingularSrc">~[wc:meritstore/meritPointsSingular]</script>
<script type="text/plain" id="meritUnitPluralSrc">~[wc:meritstore/meritPointsPlural]</script>
<!-- The RAW configured name, empty when a district/school has not set one. The values above
     already fall back to translated wording, which hides whether a name is configured - the
     cheer email needs to know, so it can keep its own per-language default instead. -->
<script type="text/plain" id="meritUnitSingularRawSrc">~[wc:meritstore/meritPrefNameSingular]</script>
<script type="text/plain" id="meritUnitPluralRawSrc">~[wc:meritstore/meritPrefNamePlural]</script>
<script type="text/plain" id="meritGroupPointsSrc">~[if#gp.~[wc:meritstore/meritPrefNamePlural]#]{0} ~[wc:meritstore/meritPrefNamePlural][else#gp]~[text:nicktech.merit.js.group_points_label][/if#gp]</script>
<script type="text/plain" id="meritGroupPointsOneSrc">~[if#gp1.~[wc:meritstore/meritPrefNameSingular]#]{0} ~[wc:meritstore/meritPrefNameSingular][else#gp1]~[text:nicktech.merit.js.group_points_one][/if#gp1]</script>

<script type="text/javascript">
	/* Reads one of the sinks above. Falls back to plain English when the block is missing or empty,
	   so a page that somehow loses the wildcard still renders words rather than "undefined". */
	function meritSrcText(id, fallback) {
		var el = document.getElementById(id);
		var t = el ? String(el.textContent).replace(/^\s+|\s+$/g, '') : '';
		return t === '' ? fallback : t;
	}

	var meritText = {
		notEnoughPoints:    "~[text:nicktech.merit.js.not_enough_points]",
		outOfStock:         "~[text:nicktech.merit.js.out_of_stock]",
		limitReached:       "~[text:nicktech.merit.js.limit_reached]",
		cartEmpty:          "~[text:nicktech.merit.js.cart_empty]",
		processingCheckout: "~[text:nicktech.merit.js.processing_checkout]",
		checkoutErrors:     "~[text:nicktech.merit.js.checkout_errors]",
		checkoutStockWarn:  "~[text:nicktech.merit.js.checkout_stock_warning]",
		noStudent:          "~[text:nicktech.merit.js.no_student]",
		lookingUp:          "~[text:nicktech.merit.js.looking_up]",
		barcodeNoMatch:     "~[text:nicktech.merit.js.barcode_no_match]",
		grade:              "~[text:nicktech.merit.js.grade]",
		unlimited:          "~[text:nicktech.merit.js.unlimited]",
		stockLeft:          "~[text:nicktech.merit.js.stock_left]",
		limitRemaining:     "~[text:nicktech.merit.js.limit_remaining]",
		addToCart:          "~[text:nicktech.merit.js.add_to_cart]",
		remove:             "~[text:nicktech.merit.js.remove]",
		progress:           "~[text:nicktech.merit.js.progress]",
		progressFailed:     "~[text:nicktech.merit.js.progress_with_failures]",
		doneAwarded:        "~[text:nicktech.merit.js.done_awarded]",
		doneAwardedFailed:  "~[text:nicktech.merit.js.done_awarded_with_failures]",
		pagerStatus:        "~[text:nicktech.merit.js.pager_status]",
		prev:               "~[text:nicktech.merit.js.prev]",
		next:               "~[text:nicktech.merit.js.next]",
		clickToSort:        "~[text:nicktech.merit.js.click_to_sort]",
		more:               "~[text:nicktech.merit.js.more]",
		less:               "~[text:nicktech.merit.js.less]",
		checkAll:           "~[text:nicktech.merit.js.check_all]",
		uncheckAll:         "~[text:nicktech.merit.js.uncheck_all]",
		checkFiltered:      "~[text:nicktech.merit.js.check_filtered]",
		uncheckFiltered:    "~[text:nicktech.merit.js.uncheck_filtered]",
		selectionEmpty:     "~[text:nicktech.merit.js.selection_empty]",
		chooseBehavior:     "~[text:nicktech.merit.js.choose_behavior_points]",
		chooseStudents:     "~[text:nicktech.merit.js.choose_behavior_students]",
		orderSaved:         "~[text:nicktech.merit.js.order_saved]",
		orderSavedFailed:   "~[text:nicktech.merit.js.order_saved_failed]",
		writeNotCommitted:  "~[text:nicktech.merit.js.write_not_committed]",
		selectionOtherSchools: "~[text:nicktech.merit.feedback.selection_other_schools]",
		dateRangeInvalid:   "~[text:nicktech.merit.error.date_range_invalid]",
		deleteNegativeBalance: "~[text:nicktech.merit.js.delete_negative_balance]",
		/* Read from the sinks above, never inlined - see the note there. unitSingular/unitPlural are
		   the bare words for the store front figures whose number is written by JS; the two group
		   values keep their translated form unless a district name is configured. */
		unitSingular:       meritSrcText('meritUnitSingularSrc', 'Point'),
		unitPlural:         meritSrcText('meritUnitPluralSrc', 'Points'),
		/* empty unless a name is configured - see the sink note above */
		unitSingularRaw:    meritSrcText('meritUnitSingularRawSrc', ''),
		unitPluralRaw:      meritSrcText('meritUnitPluralRawSrc', ''),
		groupPoints:        meritSrcText('meritGroupPointsSrc', '{0} Points'),
		groupPointsOne:     meritSrcText('meritGroupPointsOneSrc', '{0} Point'),
		groupUncategorized: "~[text:nicktech.merit.js.group_uncategorized]",
		groupCollapse:      "~[text:nicktech.merit.js.group_collapse]",
		groupCount:         "~[text:nicktech.merit.js.group_count]",
		gradeLabel:         "~[text:nicktech.merit.js.grade_label]",
		studentCount:       "~[text:nicktech.merit.js.student_count]",
		leaderboardEmpty:   "~[text:nicktech.merit.js.leaderboard_empty]",
		leaderboardLoading: "~[text:nicktech.merit.js.leaderboard_loading]",
		showAllStudents:    "~[text:nicktech.merit.js.show_all_students]",
		showTopOnly:        "~[text:nicktech.merit.js.show_top_only]",
		monthNames:         "~[text:nicktech.merit.js.month_names]",
		gradeTotals:        "~[text:nicktech.merit.heading.grade_totals]"
	};

	// Substitute {0}, {1}, ... into a message from the trailing arguments. Keeps word order
	// translatable, which string concatenation in the scripts would not.
	function meritMsg(template) {
		var args = Array.prototype.slice.call(arguments, 1);
		return String(template).replace(/\{(\d+)\}/g, function (m, i) {
			return args[i] === undefined ? '' : args[i];
		});
	}

	// PowerSchool's json output modifier writes an ampersand as its HTML entity (the database holds
	// the text as typed - both checked live; angle brackets and quotes are left alone), so every
	// string in a feed or a json-built roster arrives with that one character doubled up. Undo it ONE
	// level where the data arrives - strings anywhere inside the object/array are rewritten in place -
	// so .text(), escapeHtml, alerts and the redemption item_name all see the characters that were
	// typed.
	function meritUnamp(x) {
		if (typeof x === 'string') return x.replace(/&amp;/g, '&');
		if (x && typeof x === 'object') {
			for (var k in x) {
				if (Object.prototype.hasOwnProperty.call(x, k)) x[k] = meritUnamp(x[k]);
			}
		}
		return x;
	}
</script>
