"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get LOADING_CLASS_NAME () {
        return _constants1.LOADING_CLASS_NAME;
    },
    get Loading () {
        return Loading;
    },
    get PLUGIN_KEY () {
        return PLUGIN_KEY;
    },
    get PLUGIN_PRIORITY () {
        return PLUGIN_PRIORITY;
    }
});
const _base = require("../base");
const _content = require("./content");
const _constants = /*#__PURE__*/ _interop_require_wildcard(require("../../i18n/constants"));
const _constants1 = require("../../helpers/constants");
function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) {
        throw new TypeError("Cannot initialize the same private elements twice on an object");
    }
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) {
        return descriptor.get.call(receiver);
    }
    return descriptor.value;
}
function _class_apply_descriptor_set(receiver, descriptor, value) {
    if (descriptor.set) {
        descriptor.set.call(receiver, value);
    } else {
        if (!descriptor.writable) {
            throw new TypeError("attempted to set read only private field");
        }
        descriptor.value = value;
    }
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to " + action + " private field on non-instance");
    }
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
function _class_private_field_set(receiver, privateMap, value) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
    _class_apply_descriptor_set(receiver, descriptor, value);
    return value;
}
function _class_private_method_get(receiver, privateSet, fn) {
    if (!privateSet.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return fn;
}
function _class_private_method_init(obj, privateSet) {
    _check_private_redeclaration(obj, privateSet);
    privateSet.add(obj);
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const PLUGIN_KEY = 'loading';
const PLUGIN_PRIORITY = 350;
var /**
   * Dialog instance reference.
   *
   * @type {Dialog|null}
   */ _dialogPlugin = /*#__PURE__*/ new WeakMap(), /**
   * Handle dialog focus event.
   */ _onAfterDialogFocus = /*#__PURE__*/ new WeakSet();
class Loading extends _base.BasePlugin {
    static get PLUGIN_KEY() {
        return PLUGIN_KEY;
    }
    static get PLUGIN_PRIORITY() {
        return PLUGIN_PRIORITY;
    }
    static get DEFAULT_SETTINGS() {
        return {
            // eslint-disable-next-line max-len
            icon: `<svg class="${_constants1.LOADING_CLASS_NAME}__icon-svg" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16"><path stroke="currentColor" stroke-width="2" d="M15 8a7 7 0 1 1-3.5-6.062"></path></svg>`,
            title: undefined,
            description: ''
        };
    }
    static get SETTINGS_VALIDATORS() {
        return {
            icon: (value)=>typeof value === 'string',
            title: (value)=>typeof value === 'string',
            description: (value)=>typeof value === 'string'
        };
    }
    /**
   * Check if the plugin is enabled in the handsontable settings.
   *
   * @returns {boolean}
   */ isEnabled() {
        return !!this.hot.getSettings()[PLUGIN_KEY];
    }
    /**
   * Enable plugin for this Handsontable instance.
   */ enablePlugin() {
        if (this.enabled) {
            return;
        }
        if (_class_private_field_get(this, _dialogPlugin) === null) {
            _class_private_field_set(this, _dialogPlugin, this.hot.getPlugin('dialog'));
            if (!_class_private_field_get(this, _dialogPlugin)?.isEnabled()) {
                this.hot.getSettings().dialog = true;
            }
            this.hot.addHook('afterDialogFocus', ()=>_class_private_method_get(this, _onAfterDialogFocus, onAfterDialogFocus).call(this));
        }
        super.enablePlugin();
    }
    /**
   * Update plugin state after Handsontable settings update.
   */ updatePlugin() {
        this.disablePlugin();
        this.enablePlugin();
        super.updatePlugin();
    }
    /**
   * Disable plugin for this Handsontable instance.
   */ disablePlugin() {
        this.hide();
        super.disablePlugin();
    }
    /**
   * Check if loading dialog is currently visible.
   *
   * @returns {boolean}
   */ isVisible() {
        return _class_private_field_get(this, _dialogPlugin)?.isVisible() ?? false;
    }
    /**
   * Show loading dialog with optional custom options.
   *
   * @param {object} options Custom loading options.
   * @param {string} options.icon Custom loading icon.
   * @param {string} options.title Custom loading title.
   * @param {string} options.description Custom loading description.
   */ show(options = {}) {
        if (!this.isEnabled() || !_class_private_field_get(this, _dialogPlugin) || !_class_private_field_get(this, _dialogPlugin)?.isEnabled()) {
            return;
        }
        if (this.isVisible()) {
            this.update(options);
            return;
        }
        const shouldProceed = this.hot.runHooks('beforeLoadingShow');
        if (shouldProceed === false) {
            return;
        }
        this.update(options);
        _class_private_field_get(this, _dialogPlugin).show();
        this.hot.runHooks('afterLoadingShow');
    }
    /**
   * Hide loading dialog.
   */ hide() {
        if (!_class_private_field_get(this, _dialogPlugin) || !_class_private_field_get(this, _dialogPlugin)?.isEnabled() || !this.isVisible()) {
            return;
        }
        const shouldProceed = this.hot.runHooks('beforeLoadingHide');
        if (shouldProceed === false) {
            return;
        }
        _class_private_field_get(this, _dialogPlugin).hide();
        this.hot.runHooks('afterLoadingHide');
    }
    /**
   * Update loading description without hiding/showing the dialog.
   *
   * @param {object} options Custom loading options.
   * @param {string} options.icon Custom loading icon.
   * @param {string} options.title Custom loading title.
   * @param {string} options.description Custom loading description.
   */ update(options) {
        if (!this.isEnabled() || !_class_private_field_get(this, _dialogPlugin) || !_class_private_field_get(this, _dialogPlugin)?.isEnabled()) {
            return;
        }
        this.updatePluginSettings(options);
        const id = this.hot.guid;
        const icon = this.getSetting('icon');
        const title = this.getSetting('title') ?? this.hot.getTranslatedPhrase(_constants.LOADING_TITLE);
        const description = this.getSetting('description');
        const content = (0, _content.loadingContent)({
            id,
            icon,
            title,
            description
        });
        _class_private_field_get(this, _dialogPlugin).update({
            content,
            customClassName: _constants1.LOADING_CLASS_NAME,
            background: this.hot.countSourceRows() === 0 ? 'solid' : 'semi-transparent',
            a11y: {
                role: 'alertdialog',
                ariaLabelledby: `${id}-${PLUGIN_KEY}-title`,
                ariaDescribedby: description ? `${id}-${PLUGIN_KEY}-description` : undefined
            }
        });
    }
    /**
   * Destroy plugin instance.
   */ destroy() {
        _class_private_field_set(this, _dialogPlugin, null);
        super.destroy();
    }
    constructor(...args){
        super(...args), _class_private_method_init(this, _onAfterDialogFocus), _class_private_field_init(this, _dialogPlugin, {
            writable: true,
            value: void 0
        }), _class_private_field_set(this, _dialogPlugin, null);
    }
}
function onAfterDialogFocus() {
    _class_private_field_get(this, _dialogPlugin).focus();
}
