import { v4 as uuidv4 } from "uuid";
import { h, Host } from "@stencil/core";
const defaultObserverOptions = {
    root: null,
    threshold: 0,
    rootMargin: '0px 0px -150px 0px',
};
const SCROLL_ID = `pn-scroll-wrapper-id-`;
export class PnScroll {
    constructor() {
        this.observerOptions = defaultObserverOptions;
        this.behaviourClasses = 'fade slide-up';
        this.transitionDurationSeconds = 0.5;
        this.shouldLoop = false;
        this.intersectFunc = undefined;
        this.notIntersectFunc = undefined;
        this.compId = null;
    }
    hostElement;
    generateUniqueId() {
        return uuidv4();
    }
    handleIntersection(entries, observer, onIntersecting, onNotIntersecting, shouldLoop) {
        entries.forEach(entry => {
            if (!shouldLoop) {
                if (!entry.isIntersecting) {
                    onNotIntersecting?.(entry, observer);
                    return;
                }
                entry.target.classList.add('in-view');
                onIntersecting?.(entry, observer);
                observer.unobserve(entry.target);
            }
            else {
                entry.target.classList.toggle('in-view', entry.isIntersecting);
                if (entry.isIntersecting) {
                    onIntersecting?.(entry, observer);
                }
                else {
                    onNotIntersecting?.(entry, observer);
                }
            }
        });
    }
    //on load, generate unique id for this wrapper
    componentWillLoad() {
        this.compId = this.generateUniqueId();
    }
    componentDidLoad() {
        const observer = new IntersectionObserver((entries, observer) => this.handleIntersection(entries, observer, this.intersectFunc, this.notIntersectFunc, this.shouldLoop), this.observerOptions);
        const objectToObserve = document.getElementById(`${SCROLL_ID}${this.compId}`);
        observer.observe(objectToObserve);
    }
    render() {
        return (h(Host, { key: '443d7a3794f4b2292d39837ca89726bcdd2a5cee' }, h("div", { key: '341524e340709a5db403e024b448fd08087fa9c5', class: `scroll-wrapper ${this.behaviourClasses}`, id: `${SCROLL_ID}${this.compId}`, style: { '--transition-duration': `${this.transitionDurationSeconds}s` } }, h("slot", { key: '625bd566d903c15298969428a50030ee47c5568b', name: "scroll-affected" }))));
    }
    static get is() { return "pn-scroll"; }
    static get originalStyleUrls() {
        return {
            "$": ["pn-scroll.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pn-scroll.css"]
        };
    }
    static get properties() {
        return {
            "observerOptions": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IntersectionObserverInit",
                    "resolved": "IntersectionObserverInit",
                    "references": {
                        "IntersectionObserverInit": {
                            "location": "global",
                            "id": "global::IntersectionObserverInit"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "defaultValue": "defaultObserverOptions"
            },
            "behaviourClasses": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "behaviour-classes",
                "reflect": false,
                "defaultValue": "'fade slide-up'"
            },
            "transitionDurationSeconds": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "transition-duration-seconds",
                "reflect": false,
                "defaultValue": "0.5"
            },
            "shouldLoop": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "should-loop",
                "reflect": false,
                "defaultValue": "false"
            },
            "intersectFunc": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Function",
                    "resolved": "Function",
                    "references": {
                        "Function": {
                            "location": "global",
                            "id": "global::Function"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            },
            "notIntersectFunc": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Function",
                    "resolved": "Function",
                    "references": {
                        "Function": {
                            "location": "global",
                            "id": "global::Function"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                }
            }
        };
    }
    static get states() {
        return {
            "compId": {}
        };
    }
    static get elementRef() { return "hostElement"; }
}
//# sourceMappingURL=pn-scroll.js.map
