/******/ (() => { // webpackBootstrap
/**
 * Bookings CSV export — admin form.
 *
 * Mirrors WooCommerce core's wc-product-export.js: drives a multi-step batch export by
 * looping AJAX calls against `woocommerce_bookings_do_ajax_export`, then redirects to
 * the download URL when the server reports `step === 'done'`.
 *
 * @param {Function} $      jQuery.
 * @param {Window}   window Browser window.
 */
(function ($, window) {
  'use strict';

  function BookingsExportForm($form) {
    this.$form = $form;
    this.xhr = false;
    this.$form.find('.woocommerce-exporter-progress').val(0);
    this.processStep = this.processStep.bind(this);
    $form.on('submit', {
      exportForm: this
    }, this.onSubmit);

    // Reveal/hide the date inputs when "Custom date range?" is toggled.
    $form.on('change', '#woocommerce-exporter-date-range-toggle', function () {
      $form.find('.woocommerce-exporter-date-range').toggle(this.checked);
      if (!this.checked) {
        $form.find('.woocommerce-exporter-date-from, .woocommerce-exporter-date-to').val('');
      }
    });
  }
  BookingsExportForm.prototype.onSubmit = function (event) {
    event.preventDefault();
    const now = new Date();
    const filename = 'wc-bookings-export-' + now.getDate() + '-' + (now.getMonth() + 1) + '-' + now.getFullYear() + '-' + now.getTime() + '.csv';
    event.data.exportForm.$form.addClass('woocommerce-exporter__exporting');
    event.data.exportForm.$form.find('.woocommerce-exporter-progress').val(0);
    event.data.exportForm.$form.find('.woocommerce-exporter-button').prop('disabled', true);
    event.data.exportForm.processStep(1, $(this).serialize(), '', filename);
  };
  BookingsExportForm.prototype.processStep = function (step, data, columns, filename) {
    const selectedColumns = $('.woocommerce-exporter-columns').val();
    const productIds = $('.woocommerce-exporter-products').val() || [];
    const statuses = $('.woocommerce-exporter-statuses').val() || [];
    const exportMeta = $('#woocommerce-exporter-meta:checked').length ? 1 : 0;
    const applyFilters = $('#woocommerce-exporter-filtered:checked').length ? 1 : 0;
    const dateRangeOn = $('#woocommerce-exporter-date-range-toggle:checked').length ? 1 : 0;
    const dateFrom = dateRangeOn ? $('.woocommerce-exporter-date-from').val() || '' : '';
    const dateTo = dateRangeOn ? $('.woocommerce-exporter-date-to').val() || '' : '';

    // Forward hidden inputs:
    // - `booking_ids` (explicit selection) is always forwarded; it bypasses all filters server-side.
    // - List-table filter inputs are forwarded only when the "apply filters" checkbox is checked.
    const filterPayload = {};
    this.$form.find('input[type="hidden"]').each(function () {
      if (this.name === 'booking_ids') {
        filterPayload[this.name] = this.value;
        return;
      }
      if (applyFilters) {
        filterPayload[this.name] = this.value;
      }
    });
    $.ajax({
      type: 'POST',
      url: wc_bookings_export_params.ajax_url,
      dataType: 'json',
      data: $.extend({}, filterPayload, {
        form: data,
        action: 'woocommerce_bookings_do_ajax_export',
        step,
        columns,
        selected_columns: selectedColumns,
        product_ids: productIds,
        statuses,
        date_from: dateFrom,
        date_to: dateTo,
        export_meta: exportMeta,
        filename,
        security: wc_bookings_export_params.export_nonce
      }),
      success: response => {
        if (!response.success) {
          return;
        }
        if (response.data.step === 'done') {
          this.$form.find('.woocommerce-exporter-progress').val(response.data.percentage);
          window.location = response.data.url;
          setTimeout(() => {
            this.$form.removeClass('woocommerce-exporter__exporting');
            this.$form.find('.woocommerce-exporter-button').prop('disabled', false);
          }, 2000);
        } else {
          this.$form.find('.woocommerce-exporter-progress').val(response.data.percentage);
          this.processStep(parseInt(response.data.step, 10), data, response.data.columns, filename);
        }
      }
    }).fail(function (response) {
      window.console.log(response);
    });
  };
  $.fn.wc_bookings_export_form = function () {
    new BookingsExportForm(this);
    return this;
  };
  $(function () {
    $('.woocommerce-exporter').wc_bookings_export_form();
  });
})(jQuery, window);
/******/ })()
;
//# sourceMappingURL=admin-export-bookings.js.map