"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.sendMediaMessage = sendMediaMessage;
var _client = require("@xmpp/client");
// Monotonic counter — see sendTextMessage for the rationale (avoids
// Date.now() id collisions on <1ms-apart sends).
let _sendMediaSeq = 0;

/**
 * Same xmlns derivation as sendTextMessage — the server registers app
 * messages under the WSS service URL (`wss://<host>/ws`), and stanzas
 * carrying a bare hostname (or no xmlns at all) get silently dropped.
 */
const toServiceXmlns = (devServer, client) => {
  const clientService = client?.options?.service;
  if (clientService && /^wss?:\/\//.test(clientService)) {
    return clientService;
  }
  const ds = (devServer || '').trim();
  if (!ds) {
    return 'wss://xmpp.ethoradev.com/ws';
  }
  if (/^wss?:\/\//.test(ds)) {
    return ds;
  }
  if (ds.includes('://')) {
    return ds;
  }
  return `wss://${ds.replace(/^\/+|\/+$/g, '')}/ws`;
};
function sendMediaMessage(client, roomJID, data, customId, devServer) {
  const id = customId || `send-media-message-${Date.now()}-${_sendMediaSeq = _sendMediaSeq + 1 >>> 0}`;
  const dataToSend = {
    xmlns: toServiceXmlns(devServer, client),
    senderJID: client.jid?.toString(),
    senderFirstName: data.firstName,
    senderLastName: data.lastName,
    senderWalletAddress: data.walletAddress,
    isSystemMessage: false,
    tokenAmount: '0',
    receiverMessageId: '0',
    mucname: data.chatName,
    photoURL: data.userAvatar ? data.userAvatar : '',
    isMediafile: true,
    createdAt: data.createdAt,
    expiresAt: data.expiresAt,
    fileName: data.fileName,
    isVisible: data.isVisible,
    location: data.location,
    locationPreview: data.locationPreview,
    mimetype: data.mimetype,
    originalName: data.originalName,
    ownerKey: data.ownerKey,
    size: data.size,
    duration: data?.duration,
    updatedAt: data.updatedAt,
    userId: data.userId,
    waveForm: data.waveForm,
    attachmentId: data?.attachmentId,
    isReply: data?.isReply,
    showInChannel: data?.showInChannel,
    mainMessage: data?.mainMessage,
    roomJid: data?.roomJid,
    push: 'true'
  };
  const message = (0, _client.xml)('message', {
    id: id,
    type: 'groupchat',
    from: client.jid?.toString(),
    to: roomJID
  }, (0, _client.xml)('body', {}, 'media'), (0, _client.xml)('store', {
    xmlns: 'urn:xmpp:hints'
  }), (0, _client.xml)('data', dataToSend));
  try {
    const sendResult = client.send(message);
    if (sendResult && typeof sendResult.then === 'function') {
      sendResult.then(() => console.log('client.send resolved', {
        id
      })).catch(err => console.error('client.send REJECTED', {
        id,
        message: err?.message
      }));
    }
  } catch (error) {
    console.error(error);
  }
  return id;
}
//# sourceMappingURL=sendMediaMessage.xmpp.js.map