(function (global) {
  global.ARQ = global.ARQ || {};
  ARQ.dialogExport = {
    open: function () {
      var $body = $('<div/>').append(
        $('<p class="arq-muted"/>').text('Elige formato de exportación.'),
        $('<div style="display:flex;flex-direction:column;gap:.5rem"/>').append(
          $('<button type="button" class="arq-btn arq-btn--secondary" id="exp-png"/>').text('PNG — hoja actual'),
          $('<button type="button" class="arq-btn arq-btn--secondary" id="exp-pdf"/>').text('PDF — hoja actual'),
          $('<button type="button" class="arq-btn arq-btn--secondary" id="exp-html"/>').text('HTML — vista hoja')
        )
      );
      var dlg = ARQ.dialog.open({
        title: 'Exportar',
        body: $body,
        buttons: [{ label: 'Cerrar', onClick: function () { dlg.close(); } }],
      });
      $('#exp-png').on('click', function () { ARQ.exportPng.run(); dlg.close(); });
      $('#exp-pdf').on('click', function () { ARQ.exportPdf.run(); dlg.close(); });
      $('#exp-html').on('click', function () { ARQ.exportHtml.run(); dlg.close(); });
    },
  };
  ARQ.dialogShare = {
    show: function (relPath) {
      var base = global.location.href.replace(/[^/]+$/, '');
      var url = base + '../' + String(relPath).replace(/^\.?\//, '');
      var $code = $('<code style="word-break:break-all;display:block;padding:.5rem;background:var(--arq-bg);border-radius:8px"/>').text(url);
      var dlg = ARQ.dialog.open({
        title: 'Enlace del archivo',
        body: $('<div/>').append($('<p class="arq-muted"/>').text('Copia o abre la URL del export guardado:'), $code),
        buttons: [
          { label: 'Cerrar', onClick: function () { dlg.close(); } },
          { label: 'Abrir pestaña', onClick: function () { global.open(url, '_blank'); } },
          { label: 'Copiar enlace', primary: true, onClick: function () {
            if (navigator.clipboard && navigator.clipboard.writeText) {
              navigator.clipboard.writeText(url).then(function () {
                ARQ.bus.emit(ARQ.EVENTS.TOAST, 'Enlace copiado');
              }).catch(function () { ARQ.bus.emit(ARQ.EVENTS.TOAST, 'No se pudo copiar'); });
            }
          } },
        ],
      });
    },
  };
})(typeof window !== 'undefined' ? window : this);
