import { v4 as uuid } from 'uuid';
import { getStorageString, setStorageString } from "./storage";
import { compareVersion, isVersionInStringRange } from "./versionCompare";
export function isWebPlatform(platform) {
  if (platform) {
    return platform.toUpperCase().includes('Web'.toUpperCase()) || platform === '';
  }
  return true;
}
export function findUpgradeVersion(upgradeItems, sdkInfo) {
  var sortedItems = upgradeItems.sort(function (a, b) {
    return compareVersion(b.appVersion, a.appVersion);
  });
  //   console.log("sortedItems",sortedItems)
  var upgradeItem = sortedItems.find(function (item) {
    if (isVersionInStringRange(sdkInfo.sdkVersion, item.v) && isWebPlatform(item.platform)) {
      var _item$productScene, _sdkInfo$productScene, _item$mid, _sdkInfo$mid, _sdkInfo$greyscale;
      var matchProductScene = (_item$productScene = item.productScene) !== null && _item$productScene !== void 0 && _item$productScene.trim() ? item.productScene.toUpperCase().includes(sdkInfo === null || sdkInfo === void 0 || (_sdkInfo$productScene = sdkInfo.productScene) === null || _sdkInfo$productScene === void 0 ? void 0 : _sdkInfo$productScene.toUpperCase()) : true;
      var matchMid = (_item$mid = item.mid) !== null && _item$mid !== void 0 && _item$mid.trim() ? item.mid.toUpperCase().includes(sdkInfo === null || sdkInfo === void 0 || (_sdkInfo$mid = sdkInfo.mid) === null || _sdkInfo$mid === void 0 ? void 0 : _sdkInfo$mid.toUpperCase()) : true;
      var matchGreyscale = isGreyscaleMatch(item, (_sdkInfo$greyscale = sdkInfo.greyscale) !== null && _sdkInfo$greyscale !== void 0 ? _sdkInfo$greyscale : getGrayscaleValue());
      if (matchProductScene && matchMid && matchGreyscale) {
        return item;
      }
    }
  });
  return (upgradeItem === null || upgradeItem === void 0 ? void 0 : upgradeItem.appVersion) || null;
}
export var getGrascaleId = function getGrascaleId() {
  var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'AntomDefaultGrascaleId';
  try {
    if (key && window) {
      var storageValue = getStorageString(key, '');
      if (storageValue) {
        return storageValue;
      }
      var grascaleId = uuid();
      if (key && grascaleId) {
        setStorageString(key, grascaleId);
      }
    }
  } catch (error) {
    return '';
  }
};
export function getGrayscaleValue() {
  var seed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGrascaleId();
  if (!seed) {
    return 0;
  }
  // 取倒数2、3位
  var sub = seed.substring(seed.length - 3, seed.length - 1);
  // 将16进制转成10进制值
  var decimal = parseInt(sub, 16);
  var finalValue = Math.floor(decimal * 100 / 255);
  return finalValue;
}
export function isGreyscaleMatch(item, randomGreyscale) {
  if (!(item !== null && item !== void 0 && item.greyscale)) {
    return true;
  }
  try {
    var greyscale = parseInt(item === null || item === void 0 ? void 0 : item.greyscale);
    if (greyscale === 0) {
      return false;
    }
    return greyscale >= randomGreyscale;
  } catch (error) {
    return false;
  }
}

/**
 * 查询匹配到的版本
 * @param _extendInfo
 * @param param1
 * @returns
 */
export var getMatchAppVersion = function getMatchAppVersion(_extendInfo, sdkInfo) {
  try {
    var _info$sdkUpgradeInfo;
    var info = JSON.parse(_extendInfo);
    var versions = info === null || info === void 0 || (_info$sdkUpgradeInfo = info.sdkUpgradeInfo) === null || _info$sdkUpgradeInfo === void 0 ? void 0 : _info$sdkUpgradeInfo.versions;
    var appVersion = findUpgradeVersion(versions, sdkInfo);
    return appVersion;
  } catch (error) {
    return '';
  }
};

/**
 * 获取保存的加载版本
 * @param versionKey
 * @returns
 */
export var getLastAppVersion = function getLastAppVersion(versionKey) {
  try {
    var key = "Antom_".concat(versionKey, "_LastAppVersion");
    return getStorageString(key, '');
  } catch (error) {
    return '';
  }
};
/**
 * 保存指定场景的加载版本供下次使用
 * @param versionKey
 * @returns
 */
export var setLastAppVersion = function setLastAppVersion(versionKey, appVersion) {
  try {
    var key = "Antom_".concat(versionKey, "_LastAppVersion");
    return setStorageString(key, appVersion);
  } catch (error) {
    return false;
  }
};