var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { isDevEnvironment } from "../../engine/debug/index.js";
import { serializable } from "../../engine/engine_serialization.js";
import { DeviceUtilities } from "../../engine/engine_utils.js";
import { Behaviour } from "../Component.js";
import { ObjectRaycaster } from "../ui/Raycaster.js";
/**
 * [OpenURL](https://engine.needle.tools/docs/api/OpenURL) OpenURLMode defines how a URL should be opened.
 */
export var OpenURLMode;
(function (OpenURLMode) {
    OpenURLMode[OpenURLMode["NewTab"] = 0] = "NewTab";
    OpenURLMode[OpenURLMode["SameTab"] = 1] = "SameTab";
    OpenURLMode[OpenURLMode["NewWindow"] = 2] = "NewWindow";
})(OpenURLMode || (OpenURLMode = {}));
/**
 * OpenURL behaviour opens a URL in a new tab or window when the object (or any if it's children) is clicked.
 *
 * @category Interactivity
 * @category Web
 * @group Components
 */
export class OpenURL extends Behaviour {
    /**
     * The URL to open.
     */
    url;
    /**
     * The mode in which the URL should be opened: NewTab, SameTab, NewWindow.
     */
    mode = OpenURLMode.NewTab;
    /**
     * If true, the URL will be opened when the object with this component is clicked.
     */
    clickable = true;
    /**
     * Opens the URL in a new tab or window.
     */
    async open() {
        if (!this.url) {
            console.warn("OpenURL: URL is not set, can't open.", this);
            return;
        }
        this._validateUrl();
        let url = this.url;
        if (!url.startsWith("mailto:") && url.includes("@")) {
            url = "mailto:" + url;
        }
        switch (this.mode) {
            case OpenURLMode.NewTab:
                if (DeviceUtilities.isSafari()) {
                    globalThis.open(url, "_blank");
                }
                else
                    globalThis.open(url, "_blank");
                break;
            case OpenURLMode.SameTab:
                // TODO: test if "same tab" now also works on iOS
                if (DeviceUtilities.isSafari() && DeviceUtilities.isiOS()) {
                    globalThis.open(url, "_top");
                }
                else
                    globalThis.open(url, "_self");
                break;
            case OpenURLMode.NewWindow:
                if (DeviceUtilities.isSafari()) {
                    globalThis.open(url, "_top");
                }
                else
                    globalThis.open(url, "_new");
                break;
        }
    }
    /** @internal */
    start() {
        const raycaster = this.gameObject.getComponentInParent(ObjectRaycaster);
        if (!raycaster)
            this.gameObject.addComponent(ObjectRaycaster);
    }
    /** @internal */
    onPointerEnter(args) {
        if (!args.used && this.clickable)
            this.context.input.setCursor("pointer");
    }
    /** @internal */
    onPointerExit() {
        if (this.clickable)
            this.context.input.unsetCursor("pointer");
    }
    /** @internal */
    onPointerClick(args) {
        if (this.clickable && !args.used && this.url?.length)
            this.open();
    }
    _validateUrl() {
        if (!this.url)
            return;
        if (this.url.startsWith("www.")) {
            if (isDevEnvironment()) {
                console.warn("URL is not valid, adding https:// to the start of the URL", this.url);
            }
            this.url = "https://" + this.url;
        }
    }
}
__decorate([
    serializable()
], OpenURL.prototype, "url", void 0);
__decorate([
    serializable()
], OpenURL.prototype, "mode", void 0);
__decorate([
    serializable()
], OpenURL.prototype, "clickable", void 0);
//# sourceMappingURL=OpenURL.js.map