/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { IconRegistry } from './icon-registry';
import { ICON_CONSTANTS } from './icon-constants';
import { createSvgFromString } from '../core/utils/svg-utils';
const _activeIconRequests = new Map();
/** Determines if the provided SVG icon content is valid */
export function sanitizeSvgContent(svgContent, customViewBox, usePartAttrs = true) {
    if (!svgContent || !svgContent.includes('svg')) {
        return '';
    }
    if (!customViewBox && !svgContent.includes('viewBox')) {
        customViewBox = `0 0 ${ICON_CONSTANTS.numbers.DEFAULT_WIDTH} ${ICON_CONSTANTS.numbers.DEFAULT_HEIGHT}`;
    }
    const svgElement = createSvgFromString(svgContent, customViewBox);
    if (usePartAttrs && svgElement) {
        svgElement.setAttribute('part', 'svg');
        svgElement.querySelectorAll('path').forEach(node => {
            node.setAttribute('part', node.tagName.toLowerCase());
        });
    }
    return svgElement ? svgElement.outerHTML : '';
}
export function getCachedIcon(key) {
    return IconRegistry.get(key);
}
export function awaitIconDefinition(key, listener) {
    IconRegistry.awaitIcon(key, listener);
}
export function removeIconListener(key, listener) {
    IconRegistry.removeListener(key, listener);
}
export function fetchIconContent(url, name) {
    // Check if we already have an active request for this URL
    let request = _activeIconRequests.get(url);
    if (!request) {
        if (typeof fetch !== 'undefined' && typeof document !== 'undefined') {
            // we don't already have a request
            request = fetch(url).then(async (response) => {
                if (response.ok) {
                    const data = await response.text() || '';
                    IconRegistry.define({ name, data });
                    _activeIconRequests.delete(url);
                    return data;
                }
                IconRegistry.define({ name, data: '' });
                return '';
            });
            // Cache this request in case of duplicate requests
            _activeIconRequests.set(url, request);
        }
        else {
            // set to empty for ssr scenarios and resolve promise
            IconRegistry.define({ name, data: '' });
            return Promise.resolve('');
        }
    }
    return request || Promise.resolve('');
}
export function sanitizeExternalType(type) {
    const isValidType = ['standard', 'extended', 'custom'].includes(type);
    return isValidType ? type : '';
}
