sap.ui.define([
    "sap/base/util/extend",
    "sap/ui/base/Object",
    "sap/suite/ui/generic/template/genericUtilities/FeLogger"
], function (
    extend,
    BaseObject,
    FeLogger
) {
    "use strict";
    var oLogger = new FeLogger("lib.TableNoDataHandler").getLogger();

    function getMethods(oController, oTemplateUtils, oNoDataConfig) {
        var sNoDataRepresentation = oController.getOwnerComponent().getAppComponent().getNoDataRepresentation();

        function fnSetNoDataMessage (oSmartTable, sScenario) {
            if (sNoDataRepresentation === "textOnly") {
                fnSetTextMessage(oSmartTable, sScenario);
            } else if (["illustration", "illustrationWithFallback"].includes(sNoDataRepresentation)) {
                fnSetIllustratedMessage(oSmartTable, sScenario);
            }
        }

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

        // Sets "illustration" based no data message
        function fnSetIllustratedMessage (oSmartTable, sScenario) {
            var sSmartTableId = oSmartTable.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, sSmartTableId);
            // Find description (this logic varies between "illustration" and "illustrationWithFallback")
            var sDescription = fnDeriveIllustrationDescription(sI18nPrefix, sSmartTableId);
            // Get illustration type from config
            var sIllustrationType = oNoDataConfig[sScenario].illustrationType;

            
            var oNoDataIllustratedMessage = oSmartTable.getNoData();
            // 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 table ${oSmartTable.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 | tableId) (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, sSmartTableId) {
            var sLatestDescriptionI18nKey = sI18nPrefix + "_ILLUSTRATION_DESCRIPTION";
            var sLatestDescription = oTemplateUtils.oCommonUtils.getContextText(sLatestDescriptionI18nKey, sSmartTableId);
            if (sNoDataRepresentation === "illustration") {
                return sLatestDescription;
            }
            // Below logic is for noDataRepresentation = "illustrationWithFallback"
            var sLegacyDescription = null;
            var sLegacyI18nKey = sI18nPrefix;
            var sTableSpecificI18nSuffix = oTemplateUtils.oCommonUtils.getSpecificI18nKeyForSmartControl(sSmartTableId);
            sTableSpecificI18nSuffix = (sTableSpecificI18nSuffix === "|") ? "" : sTableSpecificI18nSuffix; // Omit the suffix "|"
            // If table specific i18n key is available, get the customized text for that.
            if (sTableSpecificI18nSuffix) {
                sLegacyDescription = oTemplateUtils.oCommonUtils.getCustomizedTextFromApp(sLegacyI18nKey + "|" + sTableSpecificI18nSuffix);
            }
            // 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 BaseObject.extend("sap.suite.ui.generic.template.lib.TableNoDataHandler", {
        constructor: function (oController, oTemplateUtils, oNoDataConfig) {
            extend(this, getMethods(oController, oTemplateUtils, oNoDataConfig));
        }
    });
});