import CryptoJS from 'crypto-js';

function signatureByKSort(appId, appKey, command, data, timestamp = null) {
  const input = typeof data === "string" ? data : JSON.stringify(data);
  if (!timestamp) {
    timestamp = (/* @__PURE__ */ new Date()).getTime() - (/* @__PURE__ */ new Date("1970-01-01T00:00:00Z")).getTime();
  }
  const sData = {
    app_id: appId,
    app_key: appKey,
    command,
    data: input,
    timestamp
  };
  const keys = Object.keys(sData);
  keys.sort();
  let result = "";
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    const value = sData[key];
    result += key + "=" + value;
    if (i < keys.length - 1) {
      result += ",";
    }
  }
  const hash = CryptoJS.MD5(result);
  const hexString = hash.toString(CryptoJS.enc.Hex);
  var signature = hexString.toLowerCase();
  return {
    app_id: appId,
    app_key: appKey,
    command,
    data: input,
    timestamp,
    signature
  };
}

export { signatureByKSort };
