import { parseBase64ToString } from "../../util";
import { getStorageString, setStorageString } from "../../util/storage";
import { compareVersion, isVersionInStringRange } from "../../util/versionCompare";
import { getGrayscaleValue, isGreyscaleMatch, isWebPlatform } from "../../util/upgrade";
/**
 * @author 谦彧 <zhangmian.zm@alipay.com>
 * @date 2024/9/17
 */
export function convertPaymentSession(sessionData) {
  try {
    return {
      id: sessionData.split('&&').slice(0, 3).join('&&'),
      data: JSON.parse(parseBase64ToString(sessionData.split('&&')[3]))
    };
  } catch (error) {
    // parseBase64ToString 已有捕获错误
    console.error('Parsing sessionData is abnormal and may affect the payment link. Please ensure to use the new version of SDK.', error);
    return {
      id: sessionData,
      data: {}
    };
  }
}
export var getWebAppVersion = function getWebAppVersion(currentSDKMetaData) {
  var mid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var webAppVersion = findTargetWebAppVersionByConfig(currentSDKMetaData, mid, config);
  if (webAppVersion) {
    return webAppVersion;
  }
  webAppVersion = getStorageString("Antom_".concat(currentSDKMetaData.sdkVersion + currentSDKMetaData.productScene, "_LastAppVersion"), '');
  if (webAppVersion) {
    return webAppVersion;
  }
  return currentSDKMetaData.sdkVersion;
};
export var saveWebAppVersion = function saveWebAppVersion(webAppVersion, currentSDKMetaData) {
  setStorageString("Antom_".concat(currentSDKMetaData.sdkVersion + currentSDKMetaData.productScene, "_LastAppVersion"), '');
};
var findTargetWebAppVersionByConfig = function findTargetWebAppVersionByConfig(currentSDKInfo) {
  var mid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var sortedConfig = config.sort(function (a, b) {
    return compareVersion(b.appVersion, a.appVersion);
  });
  var targetConfigEntry = sortedConfig.find(function (configEntry) {
    if (isVersionInStringRange(currentSDKInfo.sdkVersion, configEntry.v) && isWebPlatform(configEntry.platform)) {
      var _configEntry$productS, _currentSDKInfo$produ, _configEntry$mid, _currentSDKInfo$greys;
      var isMatchProductScene = (_configEntry$productS = configEntry.productScene) !== null && _configEntry$productS !== void 0 && _configEntry$productS.trim() ? configEntry.productScene.toUpperCase().includes(currentSDKInfo === null || currentSDKInfo === void 0 || (_currentSDKInfo$produ = currentSDKInfo.productScene) === null || _currentSDKInfo$produ === void 0 ? void 0 : _currentSDKInfo$produ.toUpperCase()) : true;
      var isMatchMid = (_configEntry$mid = configEntry.mid) !== null && _configEntry$mid !== void 0 && _configEntry$mid.trim() ? configEntry.mid.toUpperCase().includes(mid === null || mid === void 0 ? void 0 : mid.toUpperCase()) : true;
      var matchGreyscale = isGreyscaleMatch(configEntry, (_currentSDKInfo$greys = currentSDKInfo.greyscaleId) !== null && _currentSDKInfo$greys !== void 0 ? _currentSDKInfo$greys : getGrayscaleValue());
      if (isMatchProductScene && isMatchMid && matchGreyscale) {
        return configEntry;
      }
    }
  });
  return (targetConfigEntry === null || targetConfigEntry === void 0 ? void 0 : targetConfigEntry.appVersion) || null;
};