"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageSize = void 0;
var ImageSize;
(function (ImageSize) {
    ImageSize[ImageSize["Icon"] = 128] = "Icon";
    ImageSize[ImageSize["Tiny"] = 256] = "Tiny";
    ImageSize[ImageSize["Small"] = 512] = "Small";
    ImageSize[ImageSize["Medium"] = 768] = "Medium";
    ImageSize[ImageSize["Large"] = 1024] = "Large";
    ImageSize[ImageSize["ExtraLarge"] = 1600] = "ExtraLarge";
    ImageSize[ImageSize["Original"] = 1920] = "Original";
})(ImageSize || (exports.ImageSize = ImageSize = {}));
class ImageProvider {
    constructor() {
        this.imageMap = new Map();
        /**
         * Only returns after image is successfully loaded
         */
        this.getLazyLoadedImageUrl = (url, size) => {
            const imageMap = this.imageMap;
            return new Promise((resolve) => {
                const id = this.getImageID(url, size);
                const mapUrl = this.imageMap.get(id);
                if (mapUrl) {
                    return resolve(mapUrl);
                }
                const image = new Image();
                image.addEventListener('load', () => {
                    resolve(image.src);
                    imageMap.set(id, image.src);
                });
                image.src = this.resizeImage(url, size);
            });
        };
    }
    /**
     * Converts an Image URL to a resized image URL.
     *
     * Right now we can only manipulate Roomstay hosted images in S3, so only where the source
     * image URL contains cdn.app.roomstay.io can we add the resize parameters.
     *
     * This function will simply pass through any URL not matching the criteria
     *
     * @param url string
     * @param size ImageSize
     * @returns
     */
    resizeImage(url, size) {
        if (!url)
            return '';
        const id = this.getImageID(url, size);
        // Only check if source is from roomstay cdn
        if (url.includes('cdn.app.roomstay.io')) {
            const mapUrl = this.imageMap.get(id);
            if (mapUrl && mapUrl !== '') {
                return mapUrl;
            }
            return url + '?w=' + size;
        }
        return url;
    }
    getImageID(url, size) {
        return `${url}|${size}`;
    }
}
exports.default = new ImageProvider();
//# sourceMappingURL=ImageProvider.js.map