1"use strict";
2// Copyright © 2023 650 Industries.
3// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
4var __importDefault = (this && this.__importDefault) || function (mod) {
5    return (mod && mod.__esModule) ? mod : { "default": mod };
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.install = exports.setLocationHref = void 0;
9const url_parse_1 = __importDefault(require("url-parse"));
10class DOMException extends Error {
11    constructor(message, name) {
12        super(message);
13        this.name = name;
14    }
15}
16// The differences between the definitions of `Location` and `WorkerLocation`
17// are because of the `LegacyUnforgeable` attribute only specified upon
18// `Location`'s properties. See:
19// - https://html.spec.whatwg.org/multipage/history.html#the-location-interface
20// - https://heycam.github.io/webidl/#LegacyUnforgeable
21class Location {
22    constructor(href = null) {
23        const url = new url_parse_1.default(
24        // @ts-expect-error
25        href);
26        // @ts-expect-error
27        url.username = '';
28        // @ts-expect-error
29        url.password = '';
30        Object.defineProperties(this, {
31            hash: {
32                get() {
33                    return url.hash;
34                },
35                set() {
36                    throw new DOMException(`Cannot set "location.hash".`, 'NotSupportedError');
37                },
38                enumerable: true,
39            },
40            host: {
41                get() {
42                    return url.host;
43                },
44                set() {
45                    throw new DOMException(`Cannot set "location.host".`, 'NotSupportedError');
46                },
47                enumerable: true,
48            },
49            hostname: {
50                get() {
51                    return url.hostname;
52                },
53                set() {
54                    throw new DOMException(`Cannot set "location.hostname".`, 'NotSupportedError');
55                },
56                enumerable: true,
57            },
58            href: {
59                get() {
60                    return url.href;
61                },
62                set() {
63                    throw new DOMException(`Cannot set "location.href".`, 'NotSupportedError');
64                },
65                enumerable: true,
66            },
67            origin: {
68                get() {
69                    return url.origin;
70                },
71                enumerable: true,
72            },
73            pathname: {
74                get() {
75                    return url.pathname;
76                },
77                set() {
78                    throw new DOMException(`Cannot set "location.pathname".`, 'NotSupportedError');
79                },
80                enumerable: true,
81            },
82            port: {
83                get() {
84                    return url.port;
85                },
86                set() {
87                    throw new DOMException(`Cannot set "location.port".`, 'NotSupportedError');
88                },
89                enumerable: true,
90            },
91            protocol: {
92                get() {
93                    return url.protocol;
94                },
95                set() {
96                    throw new DOMException(`Cannot set "location.protocol".`, 'NotSupportedError');
97                },
98                enumerable: true,
99            },
100            search: {
101                get() {
102                    // @ts-expect-error
103                    return url.search;
104                },
105                set() {
106                    throw new DOMException(`Cannot set "location.search".`, 'NotSupportedError');
107                },
108                enumerable: true,
109            },
110            ancestorOrigins: {
111                get() {
112                    return {
113                        length: 0,
114                        item: () => null,
115                        contains: () => false,
116                    };
117                },
118                enumerable: true,
119            },
120            assign: {
121                value: function assign() {
122                    throw new DOMException(`Cannot call "location.assign()".`, 'NotSupportedError');
123                },
124                enumerable: true,
125            },
126            reload: {
127                value: function reload() {
128                    if (process.env.NODE_ENV !== 'production') {
129                        // NOTE: This does change how native fast refresh works. The upstream metro-runtime will check
130                        // if `location.reload` exists before falling back on an implementation that is nearly identical to
131                        // this. The main difference is that on iOS there is a "reason" message sent, but at the time of writing
132                        // this, that message is unused (ref: `RCTTriggerReloadCommandNotification`).
133                        const DevSettings = require('react-native')
134                            .DevSettings;
135                        return DevSettings.reload();
136                    }
137                    else {
138                        throw new DOMException(`Cannot call "location.reload()".`, 'NotSupportedError');
139                    }
140                },
141                enumerable: true,
142            },
143            replace: {
144                value: function replace() {
145                    throw new DOMException(`Cannot call "location.replace()".`, 'NotSupportedError');
146                },
147                enumerable: true,
148            },
149            toString: {
150                value: function toString() {
151                    return url.href;
152                },
153                enumerable: true,
154            },
155            [Symbol.for('Expo.privateCustomInspect')]: {
156                value(inspect) {
157                    const object = {
158                        hash: this.hash,
159                        host: this.host,
160                        hostname: this.hostname,
161                        href: this.href,
162                        origin: this.origin,
163                        pathname: this.pathname,
164                        port: this.port,
165                        protocol: this.protocol,
166                        search: this.search,
167                    };
168                    return `${this.constructor.name} ${inspect(object)}`;
169                },
170            },
171        });
172    }
173}
174Object.defineProperties(Location.prototype, {
175    [Symbol.toString()]: {
176        value: 'Location',
177        configurable: true,
178    },
179});
180let location = undefined;
181function setLocationHref(href) {
182    location = new Location(href);
183}
184exports.setLocationHref = setLocationHref;
185function install() {
186    Object.defineProperty(global, 'Location', {
187        value: Location,
188        configurable: true,
189        writable: true,
190    });
191    Object.defineProperty(window, 'location', {
192        get() {
193            return location;
194        },
195        set() {
196            throw new DOMException(`Cannot set "location".`, 'NotSupportedError');
197        },
198        enumerable: true,
199    });
200}
201exports.install = install;
202//# sourceMappingURL=Location.native.js.map