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);
}
import { BasePlugin } from '../base/index.mjs';
import { StretchCalculator } from './calculator.mjs';
export const PLUGIN_KEY = 'stretchColumns';
export const PLUGIN_PRIORITY = 155;
var /**
   * The stretch calculator.
   *
   * @type {StretchCalculator}
   */ _stretchCalculator = /*#__PURE__*/ new WeakMap(), /**
   * The previous width of the root element. Helps to determine if the width has changed.
   *
   * @type {number | null}
   */ _previousTableWidth = /*#__PURE__*/ new WeakMap(), /**
   * It observes the root element to detect changes in its width, and if detected, then it triggers
   * the table dimension calculations. In a situation where the browser's vertical scrollbar
   * appears - caused by some external UI element, the observer triggers the render.
   *
   * @type {ResizeObserver}
   */ _resizeObserver = /*#__PURE__*/ new WeakMap(), /**
   * Hook that modifies the column width - applies by the stretching logic.
   *
   * @param {number} width The column width.
   * @param {number} column The visual column index.
   * @param {string} source The source of the modification.
   * @returns {number}
   */ _onModifyColWidth = /*#__PURE__*/ new WeakSet(), /**
   * On each before render the plugin recalculates the column widths
   * based on the chosen stretching strategy.
   *
   * @param {boolean} fullRender If `true` then the full render is in progress.
   */ _onBeforeRender = /*#__PURE__*/ new WeakSet();
/* eslint-disable jsdoc/require-description-complete-sentence */ /**
 * @plugin StretchColumns
 * @class StretchColumns
 *
 * @description
 * This plugin allows to set column widths based on their widest cells.
 *
 * By default, the plugin is declared as `'none'`, which makes it disabled (same as if it was declared as `false`).
 *
 * The plugin determines what happens when the declared grid width is different from the calculated sum of all column widths.
 *
 * ```js
 * // fit the grid to the container, by stretching only the last column
 * stretchH: 'last',
 *
 * // fit the grid to the container, by stretching all columns evenly
 * stretchH: 'all',
 * ```
 *
 * To configure this plugin see {@link Options#stretchH}.
 *
 * @example
 *
 * ::: only-for javascript
 * ```js
 * const hot = new Handsontable(document.getElementById('example'), {
 *   data: getData(),
 *   stretchH: 'all',
 * });
 * ```
 * :::
 *
 * ::: only-for react
 * ```jsx
 * const hotRef = useRef(null);
 *
 * ...
 *
 * // First, let's construct Handsontable
 * <HotTable
 *   ref={hotRef}
 *   data={getData()}
 *   stretchH={'all'}
 * />
 * ```
 * :::
 *
 * ::: only-for angular
 * ```ts
 * settings = {
 *   data: getData(),
 *   stretchH: "all",
 * };
 * ```
 *
 * ```html
 * <hot-table [settings]="settings"></hot-table>
 * ```
 * :::
 */ /* eslint-enable jsdoc/require-description-complete-sentence */ export class StretchColumns extends BasePlugin {
    static get PLUGIN_KEY() {
        return PLUGIN_KEY;
    }
    static get PLUGIN_PRIORITY() {
        return PLUGIN_PRIORITY;
    }
    static get SETTING_KEYS() {
        return true;
    }
    /**
   * Checks if the plugin is enabled in the handsontable settings. This method is executed in {@link Hooks#beforeInit}
   * hook and if it returns `true` then the {@link #enablePlugin} method is called.
   *
   * @returns {boolean}
   */ isEnabled() {
        return [
            'all',
            'last'
        ].includes(this.hot.getSettings().stretchH);
    }
    /**
   * Enables the plugin functionality for this Handsontable instance.
   */ enablePlugin() {
        if (this.enabled) {
            return;
        }
        _class_private_field_get(this, _stretchCalculator).useStrategy(this.hot.getSettings().stretchH);
        _class_private_field_get(this, _resizeObserver).observe(this.hot.rootElement);
        this.addHook('beforeRender', (...args)=>_class_private_method_get(this, _onBeforeRender, onBeforeRender).call(this, ...args));
        this.addHook('modifyColWidth', (...args)=>_class_private_method_get(this, _onModifyColWidth, onModifyColWidth).call(this, ...args), 10);
        super.enablePlugin();
    }
    /**
   * Updates the plugin's state. This method is executed when {@link Core#updateSettings} is invoked.
   */ updatePlugin() {
        _class_private_field_get(this, _stretchCalculator).useStrategy(this.hot.getSettings().stretchH);
        super.updatePlugin();
    }
    /**
   * Disables the plugin functionality for this Handsontable instance.
   */ disablePlugin() {
        super.disablePlugin();
        _class_private_field_get(this, _resizeObserver).unobserve(this.hot.rootElement);
    }
    /**
   * Gets the calculated column width based on the stretching
   * strategy defined by {@link Options#stretchH} option.
   *
   * @param {number} columnVisualIndex The visual index of the column.
   * @returns {number | null}
   */ getColumnWidth(columnVisualIndex) {
        return _class_private_field_get(this, _stretchCalculator).getStretchedWidth(columnVisualIndex);
    }
    /**
   * Destroys the plugin instance.
   */ destroy() {
        _class_private_field_get(this, _resizeObserver).disconnect();
        _class_private_field_set(this, _resizeObserver, null);
        _class_private_field_set(this, _stretchCalculator, null);
        super.destroy();
    }
    constructor(...args){
        super(...args), _class_private_method_init(this, _onModifyColWidth), _class_private_method_init(this, _onBeforeRender), _class_private_field_init(this, _stretchCalculator, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _previousTableWidth, {
            writable: true,
            value: void 0
        }), _class_private_field_init(this, _resizeObserver, {
            writable: true,
            value: void 0
        }), _class_private_field_set(this, _stretchCalculator, new StretchCalculator(this.hot)), _class_private_field_set(this, _previousTableWidth, null), _class_private_field_set(this, _resizeObserver, new ResizeObserver((entries)=>{
            requestAnimationFrame(()=>{
                if (!this.hot?.view.isHorizontallyScrollableByWindow()) {
                    return;
                }
                entries.forEach(({ contentRect })=>{
                    if (_class_private_field_get(this, _previousTableWidth) !== null && _class_private_field_get(this, _previousTableWidth) !== contentRect.width) {
                        this.hot.view.adjustElementsSize();
                        this.hot.refreshDimensions();
                    }
                    _class_private_field_set(this, _previousTableWidth, contentRect.width);
                });
            });
        }));
    }
}
function onModifyColWidth(width, column, source) {
    if (source === this.pluginName) {
        return;
    }
    const newWidth = this.getColumnWidth(column);
    if (typeof newWidth === 'number') {
        return newWidth;
    }
    return width;
}
function onBeforeRender(fullRender) {
    if (fullRender) {
        _class_private_field_get(this, _stretchCalculator).refreshStretching();
    }
}
