'use client';
import { createElement, forwardRef, useState, useEffect } from 'react';
/**
* Resolve icon set icons
*
* Returns parent icon for each icon
*/
function getIconsTree(data, names) {
const icons = data.icons;
const aliases = data.aliases || Object.create(null);
const resolved = Object.create(null);
function resolve(name) {
if (icons[name]) return resolved[name] = [];
if (!(name in resolved)) {
resolved[name] = null;
const parent = aliases[name] && aliases[name].parent;
const value = parent && resolve(parent);
if (value) resolved[name] = [parent].concat(value);
}
return resolved[name];
}
(Object.keys(icons).concat(Object.keys(aliases))).forEach(resolve);
return resolved;
}
/**
* Default values for dimensions
*/
const defaultIconDimensions = Object.freeze({
left: 0,
top: 0,
width: 16,
height: 16
});
/**
* Default values for transformations
*/
const defaultIconTransformations = Object.freeze({
rotate: 0,
vFlip: false,
hFlip: false
});
/**
* Default values for all optional IconifyIcon properties
*/
const defaultIconProps = Object.freeze({
...defaultIconDimensions,
...defaultIconTransformations
});
/**
* Default values for all properties used in ExtendedIconifyIcon
*/
const defaultExtendedIconProps = Object.freeze({
...defaultIconProps,
body: "",
hidden: false
});
/**
* Merge transformations
*/
function mergeIconTransformations(obj1, obj2) {
const result = {};
if (!obj1.hFlip !== !obj2.hFlip) result.hFlip = true;
if (!obj1.vFlip !== !obj2.vFlip) result.vFlip = true;
const rotate = ((obj1.rotate || 0) + (obj2.rotate || 0)) % 4;
if (rotate) result.rotate = rotate;
return result;
}
/**
* Merge icon and alias
*
* Can also be used to merge default values and icon
*/
function mergeIconData(parent, child) {
const result = mergeIconTransformations(parent, child);
for (const key in defaultExtendedIconProps) if (key in defaultIconTransformations) {
if (key in parent && !(key in result)) result[key] = defaultIconTransformations[key];
} else if (key in child) result[key] = child[key];
else if (key in parent) result[key] = parent[key];
return result;
}
/**
* Get icon data, using prepared aliases tree
*/
function internalGetIconData(data, name, tree) {
const icons = data.icons;
const aliases = data.aliases || Object.create(null);
let currentProps = {};
function parse(name$1) {
currentProps = mergeIconData(icons[name$1] || aliases[name$1], currentProps);
}
parse(name);
tree.forEach(parse);
return mergeIconData(data, currentProps);
}
/**
* Extract icons from an icon set
*
* Returns list of icons that were found in icon set
*/
function parseIconSet(data, callback) {
const names = [];
if (typeof data !== "object" || typeof data.icons !== "object") return names;
if (data.not_found instanceof Array) data.not_found.forEach((name) => {
callback(name, null);
names.push(name);
});
const tree = getIconsTree(data);
for (const name in tree) {
const item = tree[name];
if (item) {
callback(name, internalGetIconData(data, name, item));
names.push(name);
}
}
return names;
}
/**
* Optional properties
*/
const optionalPropertyDefaults = {
provider: "",
aliases: {},
not_found: {},
...defaultIconDimensions
};
/**
* Check props
*/
function checkOptionalProps(item, defaults) {
for (const prop in defaults) if (prop in item && typeof item[prop] !== typeof defaults[prop]) return false;
return true;
}
/**
* Validate icon set, return it as IconifyJSON on success, null on failure
*
* Unlike validateIconSet(), this function is very basic.
* It does not throw exceptions, it does not check metadata, it does not fix stuff.
*/
function quicklyValidateIconSet(obj) {
if (typeof obj !== "object" || obj === null) return null;
const data = obj;
if (typeof data.prefix !== "string" || !obj.icons || typeof obj.icons !== "object") return null;
if (!checkOptionalProps(obj, optionalPropertyDefaults)) return null;
const icons = data.icons;
for (const name in icons) {
const icon = icons[name];
if (!name || typeof icon.body !== "string" || !checkOptionalProps(icon, defaultExtendedIconProps)) return null;
}
const aliases = data.aliases || Object.create(null);
for (const name in aliases) {
const icon = aliases[name];
const parent = icon.parent;
if (!name || typeof parent !== "string" || !icons[parent] && !aliases[parent] || !checkOptionalProps(icon, defaultExtendedIconProps)) return null;
}
return data;
}
/**
* Storage by provider and prefix
*/
const dataStorage = Object.create(null);
/**
* Create new storage
*/
function newStorage(provider, prefix) {
return {
provider,
prefix,
icons: Object.create(null),
missing: /* @__PURE__ */ new Set()
};
}
/**
* Get storage for provider and prefix
*/
function getStorage(provider, prefix) {
const providerStorage = dataStorage[provider] || (dataStorage[provider] = Object.create(null));
return providerStorage[prefix] || (providerStorage[prefix] = newStorage(provider, prefix));
}
/**
* Add icon set to storage
*
* Returns array of added icons
*/
function addIconSet(storage, data) {
if (!quicklyValidateIconSet(data)) return [];
return parseIconSet(data, (name, icon) => {
if (icon) storage.icons[name] = icon;
else storage.missing.add(name);
});
}
/**
* Add icon to storage
*/
function addIconToStorage(storage, name, icon) {
try {
if (typeof icon.body === "string") {
storage.icons[name] = { ...icon };
return true;
}
} catch (err) {}
return false;
}
/**
* List available icons
*/
function listIcons(provider, prefix) {
let allIcons = [];
const providers = typeof provider === "string" ? [provider] : Object.keys(dataStorage);
providers.forEach((provider$1) => {
const prefixes = typeof provider$1 === "string" && typeof prefix === "string" ? [prefix] : Object.keys(dataStorage[provider$1] || {});
prefixes.forEach((prefix$1) => {
const storage = getStorage(provider$1, prefix$1);
allIcons = allIcons.concat(Object.keys(storage.icons).map((name) => (provider$1 !== "" ? "@" + provider$1 + ":" : "") + prefix$1 + ":" + name));
});
});
return allIcons;
}
/**
* Expression to test part of icon name.
*
* Used when loading icons from Iconify API due to project naming convension.
* Ignored when using custom icon sets - convension does not apply.
*/
const matchIconName = /^[a-z0-9]+(-[a-z0-9]+)*$/;
/**
* Convert string icon name to IconifyIconName object.
*/
const stringToIcon = (value, validate, allowSimpleName, provider = "") => {
const colonSeparated = value.split(":");
if (value.slice(0, 1) === "@") {
if (colonSeparated.length < 2 || colonSeparated.length > 3) return null;
provider = colonSeparated.shift().slice(1);
}
if (colonSeparated.length > 3 || !colonSeparated.length) return null;
if (colonSeparated.length > 1) {
const name$1 = colonSeparated.pop();
const prefix = colonSeparated.pop();
const result = {
provider: colonSeparated.length > 0 ? colonSeparated[0] : provider,
prefix,
name: name$1
};
return validate && !validateIconName(result) ? null : result;
}
const name = colonSeparated[0];
const dashSeparated = name.split("-");
if (dashSeparated.length > 1) {
const result = {
provider,
prefix: dashSeparated.shift(),
name: dashSeparated.join("-")
};
return validate && !validateIconName(result) ? null : result;
}
if (allowSimpleName && provider === "") {
const result = {
provider,
prefix: "",
name
};
return validate && !validateIconName(result, allowSimpleName) ? null : result;
}
return null;
};
/**
* Check if icon is valid.
*
* This function is not part of stringToIcon because validation is not needed for most code.
*/
const validateIconName = (icon, allowSimpleName) => {
if (!icon) return false;
return !!((allowSimpleName && icon.prefix === "" || !!icon.prefix) && !!icon.name);
};
/**
* Allow storing icons without provider or prefix, making it possible to store icons like "home"
*/
let simpleNames = false;
function allowSimpleNames(allow) {
if (typeof allow === "boolean") simpleNames = allow;
return simpleNames;
}
/**
* Get icon data
*
* Returns:
* - IconifyIcon on success, object directly from storage so don't modify it
* - null if icon is marked as missing (returned in `not_found` property from API, so don't bother sending API requests)
* - undefined if icon is missing in storage
*/
function getIconData(name) {
const icon = typeof name === "string" ? stringToIcon(name, true, simpleNames) : name;
if (icon) {
const storage = getStorage(icon.provider, icon.prefix);
const iconName = icon.name;
return storage.icons[iconName] || (storage.missing.has(iconName) ? null : void 0);
}
}
/**
* Add one icon
*/
function addIcon(name, data) {
const icon = stringToIcon(name, true, simpleNames);
if (!icon) return false;
const storage = getStorage(icon.provider, icon.prefix);
if (data) return addIconToStorage(storage, icon.name, data);
else {
storage.missing.add(icon.name);
return true;
}
}
/**
* Add icon set
*/
function addCollection(data, provider) {
if (typeof data !== "object") return false;
if (typeof provider !== "string") provider = data.provider || "";
if (simpleNames && !provider && !data.prefix) {
let added = false;
if (quicklyValidateIconSet(data)) {
data.prefix = "";
parseIconSet(data, (name, icon) => {
if (addIcon(name, icon)) added = true;
});
}
return added;
}
const prefix = data.prefix;
if (!validateIconName({
prefix,
name: "a"
})) return false;
const storage = getStorage(provider, prefix);
return !!addIconSet(storage, data);
}
/**
* Check if icon data is available
*/
function iconLoaded(name) {
return !!getIconData(name);
}
/**
* Get full icon
*/
function getIcon(name) {
const result = getIconData(name);
return result ? {
...defaultIconProps,
...result
} : result;
}
/**
* Default icon customisations values
*/
const defaultIconSizeCustomisations = Object.freeze({
width: null,
height: null
});
const defaultIconCustomisations = Object.freeze({
...defaultIconSizeCustomisations,
...defaultIconTransformations
});
/**
* Regular expressions for calculating dimensions
*/
const unitsSplit = /(-?[0-9.]*[0-9]+[0-9.]*)/g;
const unitsTest = /^-?[0-9.]*[0-9]+[0-9.]*$/g;
function calculateSize(size, ratio, precision) {
if (ratio === 1) return size;
precision = precision || 100;
if (typeof size === "number") return Math.ceil(size * ratio * precision) / precision;
if (typeof size !== "string") return size;
const oldParts = size.split(unitsSplit);
if (oldParts === null || !oldParts.length) return size;
const newParts = [];
let code = oldParts.shift();
let isNumber = unitsTest.test(code);
while (true) {
if (isNumber) {
const num = parseFloat(code);
if (isNaN(num)) newParts.push(code);
else newParts.push(Math.ceil(num * ratio * precision) / precision);
} else newParts.push(code);
code = oldParts.shift();
if (code === void 0) return newParts.join("");
isNumber = !isNumber;
}
}
function splitSVGDefs(content, tag = "defs") {
let defs = "";
const index = content.indexOf("<" + tag);
while (index >= 0) {
const start = content.indexOf(">", index);
const end = content.indexOf("" + tag);
if (start === -1 || end === -1) break;
const endEnd = content.indexOf(">", end);
if (endEnd === -1) break;
defs += content.slice(start + 1, end).trim();
content = content.slice(0, index).trim() + content.slice(endEnd + 1);
}
return {
defs,
content
};
}
/**
* Merge defs and content
*/
function mergeDefsAndContent(defs, content) {
return defs ? "" + defs + "" + content : content;
}
/**
* Wrap SVG content, without wrapping definitions
*/
function wrapSVGContent(body, start, end) {
const split = splitSVGDefs(body);
return mergeDefsAndContent(split.defs, start + split.content + end);
}
/**
* Check if value should be unset. Allows multiple keywords
*/
const isUnsetKeyword = (value) => value === "unset" || value === "undefined" || value === "none";
/**
* Get SVG attributes and content from icon + customisations
*
* Does not generate style to make it compatible with frameworks that use objects for style, such as React.
* Instead, it generates 'inline' value. If true, rendering engine should add verticalAlign: -0.125em to icon.
*
* Customisations should be normalised by platform specific parser.
* Result should be converted to