
import HashMap from '@ohos.util.HashMap';
import { Base64Util } from './Base64Util';


export class ChangeUtil {
  /**
   * 将HashMap转成JsonString
   * @param map
   * @returns
   */
  static map2Json(map:HashMap<string, string>): string {
    let jsonObject: Record<string, Object> = {};
    map.forEach((value, key) => {
      if(key != undefined && value != undefined){
        jsonObject[key] = value;
      }
    })
    return JSON.stringify(jsonObject);
  }

  static getSign(extraDatas1:HashMap<string, string>): string {
    let secret= extraDatas1.get("timestamp")
    if(secret!=null) {
      let keyValueStr: string = "";
      let entriesArray: Array<string> = Array.from(extraDatas1.keys());
      entriesArray.sort();

      let sortedMap:HashMap<string, string>  = new HashMap();
      entriesArray.forEach((value: string, index: number) => {
        sortedMap.set(value,extraDatas1.get(value));
        keyValueStr +=value+extraDatas1.get(value)
      });
      keyValueStr = keyValueStr.replace(" ", "");
      keyValueStr = keyValueStr + CryptoJS.MD5(secret).toString();
      let Md5keyValueStr: string = CryptoJS.MD5(keyValueStr).toString();
      let base64Str:string=Base64Util.encodeToStrSync(Md5keyValueStr);
      return base64Str;
    }
    else
    {
      return '';
    }


  }


  static isMobileNum(mobiles:string): boolean {
    const reg2: RegExp = new RegExp('^(1[3-9])[0-9]{9}$')

  return reg2.test(mobiles);
}
  static isPassword(password:string): boolean {
    const reg2: RegExp = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$')

    return reg2.test(password);
  }


}