/******/ (() => { // webpackBootstrap
/* global ajaxurl */
/**
 * Bookings CSV import — admin form.
 *
 * Mirrors WooCommerce Gift Cards' wc-gc-import.js: drives a multi-step batch import by
 * looping AJAX calls against `woocommerce_bookings_do_ajax_import`, then redirects to
 * the "done" URL when the server reports `position === 'done'`.
 *
 * @param {Function} $      jQuery.
 * @param {Window}   window Browser window.
 */
(function ($, window) {
  'use strict';

  function BookingsImportForm($form) {
    this.$form = $form;
    this.xhr = false;
    this.mapping = wc_bookings_import_params.mapping;
    this.position = 0;
    this.file = wc_bookings_import_params.file;
    this.update_existing = wc_bookings_import_params.update_existing;
    this.delimiter = wc_bookings_import_params.delimiter;
    this.security = wc_bookings_import_params.import_nonce;
    this.imported = 0;
    this.failed = 0;
    this.updated = 0;
    this.skipped = 0;
    this.$form.find('.woocommerce-importer-progress').val(0);
    this.run_import = this.run_import.bind(this);
    this.run_import();
  }
  BookingsImportForm.prototype.run_import = function () {
    $.ajax({
      type: 'POST',
      url: ajaxurl,
      data: {
        action: 'woocommerce_bookings_do_ajax_import',
        position: this.position,
        mapping: this.mapping,
        file: this.file,
        update_existing: this.update_existing,
        delimiter: this.delimiter,
        security: this.security
      },
      dataType: 'json',
      success: response => {
        if (!response.success) {
          return;
        }
        this.position = response.data.position;
        this.imported += response.data.imported;
        this.failed += response.data.failed;
        this.updated += response.data.updated;
        this.skipped += response.data.skipped;
        this.$form.find('.woocommerce-importer-progress').val(response.data.percentage);
        if (response.data.position === 'done') {
          const fileName = wc_bookings_import_params.file.split('/').pop();
          window.location = response.data.url + '&bookings-imported=' + parseInt(this.imported, 10) + '&bookings-failed=' + parseInt(this.failed, 10) + '&bookings-updated=' + parseInt(this.updated, 10) + '&bookings-skipped=' + parseInt(this.skipped, 10) + '&file-name=' + encodeURIComponent(fileName);
        } else {
          this.run_import();
        }
      }
    }).fail(function (response) {
      window.console.log(response);
    });
  };
  $.fn.wc_bookings_importer = function () {
    new BookingsImportForm(this);
    return this;
  };
  $(function () {
    $('.woocommerce-importer').wc_bookings_importer();
  });
})(jQuery, window);
/******/ })()
;
//# sourceMappingURL=admin-import-bookings.js.map