var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _NavigateToUrlEffectHandler_queryString, _NavigateToUrlEffectHandler_parentWindowInfo, _NavigateToUrlEffectHandler_dialog;
import { EffectHandler, postEffectToWindow } from './effect-handler.js';
const IPV4_PATTERN = 
// eslint-disable-next-line security/detect-unsafe-regex
/((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])/;
const IPV6_PATTERN = 
// eslint-disable-next-line security/detect-unsafe-regex
/(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))/;
const INTERNAL_HOST_SET = new Set([
    'redditblog.com',
    'i.redd.it',
    'v.redd.it',
    'i.redditmedia.com',
    'g.redditmedia.com',
    'i.reddituploads.com',
    'www.redditinc.com',
    'reddit.onelink.me',
    'www.reddithelp.com',
    'reddit.com',
    'redditinc.com',
    'support.reddithelp.com',
    'redditforcommunity.com',
]);
const REDDIT_HOST_PATTERN = /^.*\.reddit\.com$/;
export const isRedditUrl = (url) => REDDIT_HOST_PATTERN.test(url);
export const isInternalRedditUrl = (url) => {
    try {
        return INTERNAL_HOST_SET.has(url.host) || isRedditUrl(url.host);
    }
    catch (err) {
        return false;
    }
};
export class NavigateToUrlEffectHandler extends EffectHandler {
    constructor(host, queryString = 'devvit-navigate-offsite-dialog', parentWindowInfo) {
        super(host);
        _NavigateToUrlEffectHandler_queryString.set(this, void 0);
        _NavigateToUrlEffectHandler_parentWindowInfo.set(this, void 0);
        _NavigateToUrlEffectHandler_dialog.set(this, void 0);
        __classPrivateFieldSet(this, _NavigateToUrlEffectHandler_queryString, queryString, "f");
        __classPrivateFieldSet(this, _NavigateToUrlEffectHandler_parentWindowInfo, parentWindowInfo, "f");
    }
    hostUpdated() {
        __classPrivateFieldSet(this, _NavigateToUrlEffectHandler_dialog, this.host.renderRoot.querySelector(__classPrivateFieldGet(this, _NavigateToUrlEffectHandler_queryString, "f")), "f");
    }
    onEvent(ev) {
        const { navigateToUrl } = ev.detail.effect;
        if (!navigateToUrl)
            return;
        const url = parseNavigateToURL(navigateToUrl.url);
        if (!url)
            return;
        if (__classPrivateFieldGet(this, _NavigateToUrlEffectHandler_parentWindowInfo, "f")) {
            postEffectToWindow(__classPrivateFieldGet(this, _NavigateToUrlEffectHandler_parentWindowInfo, "f"), {
                url: url.href,
                effectName: 'devvit:navigateToEffect',
            });
            return;
        }
        if (isInternalRedditUrl(url)) {
            window.open(url, '_self');
        }
        else {
            __classPrivateFieldGet(this, _NavigateToUrlEffectHandler_dialog, "f")?.show(url.toString());
        }
    }
}
_NavigateToUrlEffectHandler_queryString = new WeakMap(), _NavigateToUrlEffectHandler_parentWindowInfo = new WeakMap(), _NavigateToUrlEffectHandler_dialog = new WeakMap();
/** @internal */
export function parseNavigateToURL(url) {
    let parsed;
    try {
        parsed = new URL(url);
    }
    catch {
        console.warn(`NavigateTo malformed URL: ${url}`);
        return;
    }
    // Strip auth segments.
    if (parsed.username)
        parsed.username = '';
    if (parsed.password)
        parsed.password = '';
    // Enforce HTTPS.
    if (parsed.protocol !== 'https:') {
        console.warn(`NavigateTo insecure URL forbidden: ${url}`);
        return;
    }
    // Disallow IP address hosts.
    if (parsed.hostname.match(IPV4_PATTERN) || parsed.hostname.match(IPV6_PATTERN)) {
        console.warn(`NavigateTo IP URL forbidden: ${url}`);
        return;
    }
    // Disallow extended unicode hostnames.
    if (parsed.hostname.startsWith('xn--')) {
        console.warn(`NavigateTo extended hostname URL forbidden: ${url}`);
        return;
    }
    return parsed;
}
//# sourceMappingURL=navigate-to-url-effect-handler.js.map