import { getItemApiUrl } from "../urls/get-item-api-url";
/**
 * Get the fully qualified URL for an item's thumbnail
 * @param item w/ id, thumbnail, and access
 * @param portalUrlOrObject a portal base or API URL, a portal object, or request options containing either of those
 * @param optionsOrToken options including width and/or token for the current user's session; will only be appended as a query parameter if the item's access is **not** `public`
 * @returns URL to the item's thumbnail, defaults to `https://www.arcgis.com/sharing/rest/content/items/{item.id}/info/{item.thumbnail}`. Returns `null` if the item does not have a thumbnail assigned.
 */
export function getItemThumbnailUrl(item, portalUrlOrObject, optionsOrToken) {
    if (!item || !item.thumbnail) {
        // TODO: handle image types by returning the image (item data) itself?
        return null;
    }
    // eslint-disable-next-line prefer-const
    let { token, width } = optionsOrToken || {};
    // TODO: at the next breaking change drop support for passing token as string
    if (!token && typeof optionsOrToken === "string") {
        token = optionsOrToken;
    }
    const itemApiUrl = getItemApiUrl(item, portalUrlOrObject, token);
    const [baseUrl, search] = itemApiUrl.split("?");
    const searchParams = new URLSearchParams(search);
    searchParams.delete("f");
    if (width) {
        searchParams.append("w", String(width));
    }
    const newSearch = searchParams.toString();
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
    const url = `${baseUrl}/info/${item.thumbnail}`;
    return newSearch ? `${url}?${newSearch}` : url;
}
//# sourceMappingURL=get-item-thumbnail-url.js.map