sap.ui.define([
	"sap/suite/ui/generic/template/genericUtilities/FeLogger"
], function (
	FeLogger
) {
	"use strict";
	var oLogger = new FeLogger("lib.BaseNoDataHandler").getLogger();

	/**
 * Base handler for setting no data messages on smart controls
 * @param {object} oController - The controller instance
 * @param {object} oTemplateUtils - Template utilities
 * @param {object} oNoDataConfig - Configuration object mapping scenarios to i18n keys
 * @param {object} parentMethods - Methods provided by subclass
 * @returns {object} Public API with setNoDataMessage method
 */
	function BaseNoDataHandler(oController, oTemplateUtils, oConfig) {
		var sNoDataRepresentation = oController.getOwnerComponent().getAppComponent().getNoDataRepresentation();
		var oNoDataConfig = oConfig.oNoDataConfig;
		var parentMethods = oConfig.methods;

		function fnSetNoDataMessage (oSmartCtrl, sScenario) {
			if (sNoDataRepresentation === "textOnly" && !oConfig.isIllustrationOnlyMode) {
				fnSetTextMessage(oSmartCtrl, sScenario);
			} else if (["illustration", "illustrationWithFallback"].includes(sNoDataRepresentation) || oConfig.isIllustrationOnlyMode) {
				fnSetIllustratedMessage(oSmartCtrl, sScenario);
			}
		}

		// Sets "text" based no data message
		function fnSetTextMessage (oSmartCtrl, sScenario) {
			var sSmartCtrlId = oSmartCtrl.getId();
			var sI18nKey = oNoDataConfig[sScenario].i18nKey;
			var sNoDataText = oTemplateUtils.oCommonUtils.getContextText(sI18nKey, sSmartCtrlId);
			oSmartCtrl.setNoData(sNoDataText);
		}

		// Sets "illustration" based no data message
		function fnSetIllustratedMessage (oSmartCtrl, sScenario) {
			var sSmartCtrlId = oSmartCtrl.getId();
			var sI18nPrefix = oNoDataConfig[sScenario].i18nKey;
			// Derive title key from base key
			var sTitleI18nKey = sI18nPrefix + "_ILLUSTRATION_TITLE";
			// Get title from resource bundle.
			var sTitle = oTemplateUtils.oCommonUtils.getContextText(sTitleI18nKey, sSmartCtrlId);
			// Find description (this logic varies between "illustration" and "illustrationWithFallback")
			var sDescription = fnDeriveIllustrationDescription(sI18nPrefix, sSmartCtrlId);
			// Get illustration type from config
			var sIllustrationType = oNoDataConfig[sScenario].illustrationType;

			var oNoDataIllustratedMessage = parentMethods.getIllustratedMessage(oSmartCtrl);
			// oSmartTable.getNoData should actually return an illustrated message.
			//
			// Sometimes, app changes the no data message into text message using custom coding.
			// Hence, adding this type check as a safety measure.
			if ( !(oNoDataIllustratedMessage?.isA?.("sap.m.IllustratedMessage")) ) {
				oLogger.error(`Unable to update the no data message of smart control ${oSmartCtrl.getId()} as it's not an IllustratedMessage`);
				return;
			}
			// Table already has illustrated no data message. Just update the attributes
			oNoDataIllustratedMessage.setIllustrationType(sIllustrationType);
			oNoDataIllustratedMessage.setTitle(sTitle);
			oNoDataIllustratedMessage.setDescription(sDescription);
		}

		/**
		 * Calculates the illustrated message's description based on the manifest flag "noDataRepresentation" value
		 * 1. If manifest flag is "illustration"
		 * 	  - Just returns text for the new i18n key (legacy key + "_ILLUSTRATION_DESCRIPTION")
		 * 	  - The text can be found on either app or FE
		 * 2. If flag is "illustrationWithFallback"
		 * 	  a. Checks the app if customized text available for (legacy i18n key | ctrlId) (for e.g. "NOITEMS_LR_SMARTTABLE|C_SalesOrder")
		 * 	   	 - If found, returns it.
		 * 	  b. Checks the app if customized text available just for legacy i18n key (for e.g. "NOITEMS_LR_SMARTTABLE")
		 * 	   	 - If found, returns it.
		 * 	  c. Returns the text for the new i18n key (Same as "Case 1" above).
		 */
		function fnDeriveIllustrationDescription (sI18nPrefix, sSmartCtrlId) {
			var sLatestDescriptionI18nKey = sI18nPrefix + "_ILLUSTRATION_DESCRIPTION";
			var sLatestDescription = oTemplateUtils.oCommonUtils.getContextText(sLatestDescriptionI18nKey, sSmartCtrlId);
			if (sNoDataRepresentation === "illustration" || (oConfig.isIllustrationOnlyMode && sNoDataRepresentation === "textOnly")) {
				return sLatestDescription;
			}
			// Below logic is for noDataRepresentation = "illustrationWithFallback"
			var sLegacyDescription = null;
			var sLegacyI18nKey = sI18nPrefix;
			var sCtrlSpecificI18nSuffix = oTemplateUtils.oCommonUtils.getSpecificI18nKeyForSmartControl(sSmartCtrlId);
			sCtrlSpecificI18nSuffix = (sCtrlSpecificI18nSuffix === "|") ? "" : sCtrlSpecificI18nSuffix; // Omit the suffix "|"
			// If table specific i18n key is available, get the customized text for that.
			if (sCtrlSpecificI18nSuffix) {
				sLegacyDescription = oTemplateUtils.oCommonUtils.getCustomizedTextFromApp(sLegacyI18nKey + "|" + sCtrlSpecificI18nSuffix);
			}
			// If the description is not yet found, get the customized text just for the generic i18n key.
			if (!sLegacyDescription) {
				sLegacyDescription = oTemplateUtils.oCommonUtils.getCustomizedTextFromApp(sLegacyI18nKey);
			}
			// If legacy text is found, return it.
			// Otherwise, return the latest one.
			return sLegacyDescription || sLatestDescription;
		}

		return {
			setNoDataMessage: fnSetNoDataMessage
		};
	}

	return BaseNoDataHandler;
});
