"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 PLUGIN_KEY () {
        return PLUGIN_KEY;
    },
    get PLUGIN_PRIORITY () {
        return PLUGIN_PRIORITY;
    },
    get TouchScroll () {
        return TouchScroll;
    }
});
const _element = require("../../helpers/dom/element");
const _array = require("../../helpers/array");
const _base = require("../base");
const _feature = require("../../helpers/feature");
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_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);
}
const PLUGIN_KEY = 'touchScroll';
const PLUGIN_PRIORITY = 200;
var /**
   * After view render listener.
   */ _onAfterViewRender = /*#__PURE__*/ new WeakSet(), /**
   * Touch scroll listener.
   */ _onBeforeTouchScroll = /*#__PURE__*/ new WeakSet(), /**
   * After momentum scroll listener.
   */ _onAfterMomentumScroll = /*#__PURE__*/ new WeakSet();
class TouchScroll extends _base.BasePlugin {
    static get PLUGIN_KEY() {
        return PLUGIN_KEY;
    }
    static get PLUGIN_PRIORITY() {
        return PLUGIN_PRIORITY;
    }
    static get SETTING_KEYS() {
        return true;
    }
    /**
   * Check if plugin is enabled.
   *
   * @returns {boolean}
   */ isEnabled() {
        return (0, _feature.isTouchSupported)();
    }
    /**
   * Enable the plugin.
   */ enablePlugin() {
        if (this.enabled) {
            return;
        }
        this.addHook('afterViewRender', ()=>_class_private_method_get(this, _onAfterViewRender, onAfterViewRender).call(this));
        this.registerEvents();
        super.enablePlugin();
    }
    /**
   * Updates the plugin to use the latest options you have specified.
   */ updatePlugin() {
        this.lockedCollection = false;
        super.updatePlugin();
    }
    /**
   * Disable plugin for this Handsontable instance.
   */ disablePlugin() {
        super.disablePlugin();
    }
    /**
   * Register all necessary events.
   *
   * @private
   */ registerEvents() {
        this.addHook('beforeTouchScroll', ()=>_class_private_method_get(this, _onBeforeTouchScroll, onBeforeTouchScroll).call(this));
        this.addHook('afterMomentumScroll', ()=>_class_private_method_get(this, _onAfterMomentumScroll, onAfterMomentumScroll).call(this));
    }
    constructor(...args){
        super(...args), _class_private_method_init(this, _onAfterViewRender), _class_private_method_init(this, _onBeforeTouchScroll), _class_private_method_init(this, _onAfterMomentumScroll), /**
   * Collection of scrollbars to update.
   *
   * @type {Array}
   */ this.scrollbars = [], /**
   * Collection of overlays to update.
   *
   * @type {Array}
   */ this.clones = [], /**
   * Flag which determines if collection of overlays should be refilled on every table render.
   *
   * @type {boolean}
   * @default false
   */ this.lockedCollection = false, /**
   * Flag which determines if walkontable should freeze overlays while scrolling.
   *
   * @type {boolean}
   * @default false
   */ this.freezeOverlays = false;
    }
}
function onAfterViewRender() {
    if (this.lockedCollection) {
        return;
    }
    const { topOverlay, bottomOverlay, inlineStartOverlay, topInlineStartCornerOverlay, bottomInlineStartCornerOverlay } = this.hot.view._wt.wtOverlays;
    this.lockedCollection = true;
    this.scrollbars.length = 0;
    this.scrollbars.push(topOverlay);
    if (bottomOverlay.clone) {
        this.scrollbars.push(bottomOverlay);
    }
    this.scrollbars.push(inlineStartOverlay);
    if (topInlineStartCornerOverlay) {
        this.scrollbars.push(topInlineStartCornerOverlay);
    }
    if (bottomInlineStartCornerOverlay && bottomInlineStartCornerOverlay.clone) {
        this.scrollbars.push(bottomInlineStartCornerOverlay);
    }
    this.clones = [];
    if (topOverlay.needFullRender) {
        this.clones.push(topOverlay.clone.wtTable.holder.parentNode);
    }
    if (bottomOverlay.needFullRender) {
        this.clones.push(bottomOverlay.clone.wtTable.holder.parentNode);
    }
    if (inlineStartOverlay.needFullRender) {
        this.clones.push(inlineStartOverlay.clone.wtTable.holder.parentNode);
    }
    if (topInlineStartCornerOverlay) {
        this.clones.push(topInlineStartCornerOverlay.clone.wtTable.holder.parentNode);
    }
    if (bottomInlineStartCornerOverlay && bottomInlineStartCornerOverlay.clone) {
        this.clones.push(bottomInlineStartCornerOverlay.clone.wtTable.holder.parentNode);
    }
}
function onBeforeTouchScroll() {
    this.freezeOverlays = true;
    (0, _array.arrayEach)(this.clones, (clone)=>{
        (0, _element.addClass)(clone, 'hide-tween');
    });
}
function onAfterMomentumScroll() {
    this.freezeOverlays = false;
    (0, _array.arrayEach)(this.clones, (clone)=>{
        (0, _element.removeClass)(clone, 'hide-tween');
        (0, _element.addClass)(clone, 'show-tween');
    });
    this.hot._registerTimeout(()=>{
        (0, _array.arrayEach)(this.clones, (clone)=>{
            (0, _element.removeClass)(clone, 'show-tween');
        });
    }, 400);
    (0, _array.arrayEach)(this.scrollbars, (scrollbar)=>{
        scrollbar.refresh();
        scrollbar.resetFixedPosition();
    });
    this.hot.view._wt.wtOverlays.syncScrollWithMaster();
}
