"use strict";

import { transformArrayToObject } from './transformTranslatations';
const extractTimestamp = (str, stanza) => {
  if (!str) {
    return null;
  }
  if (typeof str !== 'string') {
    console.log(str, stanza.toString());
    return null;
  }
  const timestamp = str.slice(-16);
  return timestamp;
};
export const getDataFromXml = async stanza => {
  const fullData = stanza.getChild('result')?.getChild('forwarded')?.getChild('message') || stanza;
  const xmppId = fullData?.attrs.id;
  const xmppFrom = fullData?.attrs?.from;
  const [roomJid, userWallet] = (xmppFrom || '').split('/');
  let id = stanza.getChild('result')?.attrs.id || extractTimestamp(stanza?.getChild('stanza-id')?.attrs?.id, stanza);
  if (!id) {
    id = xmppId || Date.now().toString();
  }
  const body = fullData?.getChild('body')?.getText() || undefined;
  const deleted = !!fullData?.getChild('deleted');
  const translations = fullData?.getChild('translations')?.attrs?.value ? transformArrayToObject(JSON.parse(fullData.getChild('translations').attrs.value).translates) : undefined;
  const langSource = fullData?.getChild('translate')?.attrs?.source;
  const numericPart = /\d{13,}/.exec(id || '')?.[0];
  const date = numericPart ? new Date(+numericPart.slice(0, 13)).toISOString() : new Date().toISOString();
  const data = fullData?.getChild('data') || stanza?.getChild('data');
  const photoURL = data?.attrs?.photo;
  const user = {
    id: userWallet,
    photoURL
  };
  const dataAttrs = data?.attrs || {};
  return {
    data: dataAttrs,
    id,
    body,
    roomJid,
    date,
    user,
    deleted,
    translations,
    langSource,
    xmppId,
    xmppFrom
  };
};
//# sourceMappingURL=getDataFromXml.js.map