// #region Imports
// #endregion
/**
 * A location strategy for hash-based routing.
 *
 * URLs follow the pattern `http://example.com/#/path?query#fragment`.
 * The application path lives inside `window.location.hash`, after the `#` prefix.
 *
 * This strategy is suitable for static file hosting (e.g. GitHub Pages, S3)
 * where every request must be served by the same `index.html`.
 *
 * @public
 */
export class HashLocationStrategy {
    // #region Properties
    /** @inheritdoc */
    get pathname() {
        const hash = window.location.hash;
        if (!hash || hash === '#') {
            return '/';
        }
        // hash is e.g. "#/support?q=foo#tab"
        const raw = hash.substring(1);
        // strip query params and in-route fragment
        const withoutQuery = raw.split('?')[0];
        const withoutFragment = withoutQuery.split('#')[0];
        if (!withoutFragment || !withoutFragment.startsWith('/')) {
            return `/${withoutFragment}`;
        }
        return withoutFragment;
    }
    /** @inheritdoc */
    get href() {
        return window.location.href;
    }
    /** @inheritdoc */
    get search() {
        return window.location.search;
    }
    /** @inheritdoc */
    get hash() {
        const hash = window.location.hash;
        const secondHashIndex = hash.indexOf('#', 1);
        return secondHashIndex === -1 ? '' : hash.substring(secondHashIndex + 1);
    }
    /** @inheritdoc */
    get hostname() {
        return window.location.hostname;
    }
    /** @inheritdoc */
    get origin() {
        return window.location.origin;
    }
    /** @inheritdoc */
    get host() {
        return window.location.host;
    }
    /** @inheritdoc */
    get port() {
        return window.location.port;
    }
    /** @inheritdoc */
    get protocol() {
        return window.location.protocol;
    }
    // #endregion
    // #region Methods
    /** @inheritdoc */
    pushState(path) {
        window.location.hash = `#${path}`;
    }
    /** @inheritdoc */
    replaceState(path) {
        window.history.replaceState({}, '', `${window.location.pathname}${window.location.search}#${path}`);
    }
    /** @inheritdoc */
    prepareExternalUrl(pathname) {
        return `#${pathname}`;
    }
    /** @inheritdoc */
    getSearchParam(name) {
        return this.getSearchParams().get(name);
    }
    /** @inheritdoc */
    setSearchParam(name, value) {
        const params = this.getSearchParams();
        params.set(name, value);
        this.applySearchParams(params);
    }
    /** @inheritdoc */
    deleteSearchParam(name) {
        const params = this.getSearchParams();
        params.delete(name);
        this.applySearchParams(params);
    }
    /**
     * Parses query params from inside the hash fragment (e.g. `#/path?key=val`).
     */
    getSearchParams() {
        const hash = window.location.hash.startsWith('#')
            ? window.location.hash.slice(1)
            : window.location.hash;
        const queryIndex = hash.indexOf('?');
        return new URLSearchParams(queryIndex === -1 ? '' : hash.substring(queryIndex + 1));
    }
    /**
     * Writes query params back into the hash fragment via `history.replaceState`.
     */
    applySearchParams(params) {
        const hash = window.location.hash.startsWith('#')
            ? window.location.hash.slice(1)
            : window.location.hash;
        const basePath = hash.split('?')[0];
        const search = params.toString();
        const newHash = search ? `${basePath}?${search}` : basePath;
        window.history.replaceState({}, '', `${window.location.pathname}${window.location.search}#${newHash}`);
    }
}
//# sourceMappingURL=HashLocationStrategy.js.map