import { BaseView } from "../../BaseView.js";
export default class GridDescriptionResizer extends BaseView {
    constructor(options) {
        super(options);
        this.m_parent = options.parent;
        this.state.unset("parent");
    }
    get source() {
        return super.source;
    }
    get parent() {
        return this.m_parent;
    }
    get footer() {
        return this.m_parent;
    }
    template() {
        return `<div class="-fb-grid-resizer"></div>`;
    }
    onActivate(e) {
        super.onActivate(e);
        this.node.draggable({
            axis: "y",
            containment: "parent",
            drag: (e) => {
                $(e.target).css({
                    opacity: ".3"
                });
            },
            stop: (e, ui) => {
                this.setResizerTopOnDrag($(e.target), ui);
            }
        });
        this.isBrowserResizeAware = true;
    }
    onBrowserResize() {
        this.setResizerTopOnContainerResize();
    }
    onDestroy(e) {
        super.onDestroy(e);
        if (this.node.data("ui-draggable")) {
            this.node.draggable("destroy");
        }
    }
    setResizerTopOnDrag(target = null, ui = null) {
        let parentOffsetTop = this.parent.node.offset().top;
        let calculatedTop = ui.offset.top - parentOffsetTop;
        let top = this.getDescriptionTop(calculatedTop);
        this.setPosition(target, top);
    }
    setResizerTopOnContainerResize() {
        let calculatedTop = this.parent.node.height() - this.parent.footer.node.height();
        let top = this.getDescriptionTop(calculatedTop);
        this.setPosition(this.node, top);
    }
    getDescriptionTop(top) {
        let result = top;
        let parentHeight = this.parent.node.height();
        if (top < 100) {
            result = 100;
        }
        if (top > (parentHeight - 100)) {
            result = (parentHeight) - 100;
        }
        return result;
    }
    setPosition(node, top) {
        let height = this.parent.node.height() - top;
        node.css({
            top: `${top}px`,
            opacity: 0
        });
        this.source.locals.events.descriptionResizePublish({
            height: height
        });
        this.source.locals.storage.descriptionHeight = height;
    }
}
