"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, /**
 * @param {Core} Handsontable The Handsontable instance.
 */ "default", {
    enumerable: true,
    get: function() {
        return jQueryWrapper;
    }
});
const _errors = require("../../helpers/errors");
function jQueryWrapper(Handsontable) {
    // eslint-disable-next-line
    const jQuery = typeof window === 'undefined' ? false : window.jQuery;
    if (!jQuery) {
        return;
    }
    jQuery.fn.handsontable = function(action, ...args) {
        const $this = this.first(); // Use only first element from list
        let instance = $this.data('handsontable');
        // Init case
        if (typeof action !== 'string') {
            const userSettings = action || {};
            if (instance) {
                instance.updateSettings(userSettings);
            } else {
                instance = new Handsontable.Core($this[0], userSettings);
                $this.data('handsontable', instance);
                instance.init();
            }
            return $this;
        }
        let output;
        // Action case
        if (instance) {
            if (typeof instance[action] !== 'undefined') {
                output = instance[action].call(instance, ...args);
                if (action === 'destroy') {
                    $this.removeData();
                }
            } else {
                (0, _errors.throwWithCause)(`Handsontable do not provide action: ${action}`);
            }
        }
        return output;
    };
}
