/******/ (() => { // webpackBootstrap
/* globals wc_bookings_export_params */
/**
 * 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'`.
 */
(function ($, window) {
  'use strict';

  function Bookings_Export_Form($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('');
      }
    });
  }
  Bookings_Export_Form.prototype.onSubmit = function (event) {
    event.preventDefault();
    var now = new Date();
    var 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);
  };
  Bookings_Export_Form.prototype.processStep = function (step, data, columns, filename) {
    var $this = this;
    var selected_columns = $('.woocommerce-exporter-columns').val();
    var product_ids = $('.woocommerce-exporter-products').val() || [];
    var statuses = $('.woocommerce-exporter-statuses').val() || [];
    var export_meta = $('#woocommerce-exporter-meta:checked').length ? 1 : 0;
    var apply_filters = $('#woocommerce-exporter-filtered').length ? $('#woocommerce-exporter-filtered:checked').length ? 1 : 0 : 0;
    var date_range_on = $('#woocommerce-exporter-date-range-toggle:checked').length ? 1 : 0;
    var date_from = date_range_on ? $('.woocommerce-exporter-date-from').val() || '' : '';
    var date_to = date_range_on ? $('.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.
    var filter_payload = {};
    $this.$form.find('input[type="hidden"]').each(function () {
      if ('booking_ids' === this.name) {
        filter_payload[this.name] = this.value;
        return;
      }
      if (apply_filters) {
        filter_payload[this.name] = this.value;
      }
    });
    $.ajax({
      type: 'POST',
      url: wc_bookings_export_params.ajax_url,
      dataType: 'json',
      data: $.extend({}, filter_payload, {
        form: data,
        action: 'woocommerce_bookings_do_ajax_export',
        step: step,
        columns: columns,
        selected_columns: selected_columns,
        product_ids: product_ids,
        statuses: statuses,
        date_from: date_from,
        date_to: date_to,
        export_meta: export_meta,
        filename: filename,
        security: wc_bookings_export_params.export_nonce
      }),
      success: function (response) {
        if (!response.success) {
          return;
        }
        if ('done' === response.data.step) {
          $this.$form.find('.woocommerce-exporter-progress').val(response.data.percentage);
          window.location = response.data.url;
          setTimeout(function () {
            $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 Bookings_Export_Form(this);
    return this;
  };
  $(function () {
    $('.woocommerce-exporter').wc_bookings_export_form();
  });
})(jQuery, window);
/******/ })()
;
//# sourceMappingURL=admin-export-bookings.js.map