"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestTarget = void 0;
const search_ts_1 = require("./search.js");
class RequestTarget extends URLSearchParams {
    #target;
    pathname;
    search;
    /** Target a specific record, but can be combined with select */
    id;
    /** Indicates that this is a request to query for collection of records */
    isCollection;
    /**	 The sort attribute and direction to use */
    /** @ts-expect-error USP has a sort method, we hide it */
    sort = null;
    constructor(target) {
        let searchIndex;
        let path;
        if (target && (searchIndex = target.indexOf('?')) > -1) {
            // we have query parameters that need to be parsed
            path = target.slice(0, searchIndex);
            const search = target.slice(searchIndex + 1);
            super(search);
            this.search = search;
            (0, search_ts_1.parseQuery)(search, this);
            if (!path) {
                // if there is a query string, but no path, treat as a collection anyway
                this.isCollection = true;
                this.id = null;
                return;
            }
        }
        else {
            super();
            path = target;
        }
        this.pathname = path;
        this.#target = target;
        if (path) {
            // parse for properties and set the id
            if (path.startsWith('/'))
                path = path.substring(1);
        }
        else {
            return; // leave this.id undefined
        }
        if (path) {
            if (path.endsWith('/')) {
                this.isCollection = true;
            }
            this.id = decodeURIComponent(path);
        }
        else {
            this.isCollection = true;
            this.id = null;
        }
    }
    toString() {
        if (this.#target)
            return this.#target;
        const path = this.pathname ?? this.id?.toString() ?? '';
        if (this.size > 0)
            return path + '?' + super.toString();
        else
            return path;
    }
    get url() {
        // for back-compat?
        return this.toString();
    }
    delete(name) {
        super.delete(name);
        if (this.conditions) {
            // remove any associated conditions (we may want to consider recursively going into nested conditions?)
            this.conditions = this.conditions.filter((condition) => condition.attribute !== name);
        }
        this.#target = undefined; // remove this so that we can regenerate string representation based on query params
    }
    set(name, value) {
        this.delete(name); // clear out any existing conditions and #target
        super.set(name, value);
        this.conditions?.push({ attribute: name, value });
    }
    append(name, value) {
        super.append(name, value);
        this.#target = undefined; // remove this so that we can regenerate string representation based on query params
        this.conditions?.push({ attribute: name, value });
    }
}
exports.RequestTarget = RequestTarget;
//# sourceMappingURL=RequestTarget.js.map