var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/**
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
import { Injectable } from '@angular/core';
import { ElIconPackType } from './icon-pack';
import { ElFontIcon, ElSvgIcon } from './icon';
import * as i0 from "@angular/core";
var ElIconDefinition = /** @class */ (function () {
    function ElIconDefinition() {
    }
    return ElIconDefinition;
}());
export { ElIconDefinition };
function throwPackNotFoundError(name) {
    throw Error("Icon Pack '" + name + "' is not registered");
}
function throwNoDefaultPackError() {
    throw Error('Default pack is not registered.');
}
function throwIconNotFoundError(name, pack) {
    throw Error("Icon '" + name + "' is not registered in pack '" + pack + "'. Check icon name or consider switching icon pack.");
}
function throwWrongPackTypeError(name, type, desiredType) {
    throw Error("Pack '" + name + "' is not an '" + desiredType + "' Pack and its type is '" + type + "'");
}
/**
 * This service allows to register multiple icon packs to use them later within `<el-icon></el-icon>` component.
 */
var ElIconLibraries = /** @class */ (function () {
    function ElIconLibraries() {
        this.packs = new Map();
    }
    /**
     * Registers new Svg icon pack
     * @param {string} name
     * @param {ElIcon} icons
     * @param {ElIconPackParams} params
     */
    ElIconLibraries.prototype.registerSvgPack = function (name, icons, params) {
        if (params === void 0) { params = {}; }
        this.packs.set(name, {
            name: name,
            icons: new Map(Object.entries(icons)),
            params: params,
            type: ElIconPackType.SVG,
        });
    };
    /**
     * Registers new font pack
     * @param {string} name
     * @param {ElIconPackParams} params
     */
    ElIconLibraries.prototype.registerFontPack = function (name, params) {
        if (params === void 0) { params = {}; }
        this.packs.set(name, {
            name: name,
            params: params,
            icons: new Map(),
            type: ElIconPackType.FONT,
        });
    };
    /**
     * Returns pack by name
     * @param {string} name
     */
    ElIconLibraries.prototype.getPack = function (name) {
        return this.packs.get(name);
    };
    /**
     * Sets pack as a default
     * @param {string} name
     */
    ElIconLibraries.prototype.setDefaultPack = function (name) {
        if (!this.packs.has(name)) {
            throwPackNotFoundError(name);
        }
        this.defaultPack = this.packs.get(name);
    };
    /**
     * Returns Svg icon
     * @param {string} name
     * @param {string} pack
     *
     * @returns ElIconDefinition
     */
    ElIconLibraries.prototype.getSvgIcon = function (name, pack) {
        var iconsPack = pack ? this.getPackOrThrow(pack) : this.getDefaultPackOrThrow();
        if (iconsPack.type !== ElIconPackType.SVG) {
            throwWrongPackTypeError(iconsPack.name, iconsPack.type, 'SVG');
        }
        var icon = this.getIconFromPack(name, iconsPack);
        return {
            name: name,
            pack: iconsPack.name,
            type: ElIconPackType.SVG,
            icon: this.createSvgIcon(name, icon, iconsPack.params),
        };
    };
    /**
     * Returns Font icon
     * @param {string} name
     * @param {string} pack
     *
     * @returns ElIconDefinition
     */
    ElIconLibraries.prototype.getFontIcon = function (name, pack) {
        var iconsPack = pack ? this.getPackOrThrow(pack) : this.getDefaultPackOrThrow();
        if (iconsPack.type !== ElIconPackType.FONT) {
            throwWrongPackTypeError(iconsPack.name, iconsPack.type, 'Font');
        }
        var icon = this.getIconFromPack(name, iconsPack, false);
        return {
            name: name,
            pack: iconsPack.name,
            type: ElIconPackType.FONT,
            icon: this.createFontIcon(name, icon ? icon : '', iconsPack.params),
        };
    };
    /**
     * Returns an icon
     * @param {string} name
     * @param {string} pack
     *
     * @returns ElIconDefinition
     */
    ElIconLibraries.prototype.getIcon = function (name, pack) {
        var iconsPack = pack ? this.getPackOrThrow(pack) : this.getDefaultPackOrThrow();
        if (iconsPack.type === ElIconPackType.SVG) {
            return this.getSvgIcon(name, pack);
        }
        return this.getFontIcon(name, pack);
    };
    ElIconLibraries.prototype.createSvgIcon = function (name, content, params) {
        return content instanceof ElSvgIcon ? content : new ElSvgIcon(name, content, params);
    };
    ElIconLibraries.prototype.createFontIcon = function (name, content, params) {
        return content instanceof ElFontIcon ? content : new ElFontIcon(name, content, params);
    };
    ElIconLibraries.prototype.getPackOrThrow = function (name) {
        var pack = this.packs.get(name);
        if (!pack) {
            throwPackNotFoundError(name);
        }
        return pack;
    };
    ElIconLibraries.prototype.getDefaultPackOrThrow = function () {
        if (!this.defaultPack) {
            throwNoDefaultPackError();
        }
        return this.defaultPack;
    };
    ElIconLibraries.prototype.getIconFromPack = function (name, pack, shouldThrow) {
        if (shouldThrow === void 0) { shouldThrow = true; }
        if (shouldThrow && !pack.icons.has(name)) {
            throwIconNotFoundError(name, pack.name);
        }
        return pack.icons.get(name);
    };
    ElIconLibraries.ngInjectableDef = i0.ɵɵdefineInjectable({ factory: function ElIconLibraries_Factory() { return new ElIconLibraries(); }, token: ElIconLibraries, providedIn: "root" });
    ElIconLibraries = __decorate([
        Injectable({ providedIn: 'root' })
    ], ElIconLibraries);
    return ElIconLibraries;
}());
export { ElIconLibraries };
//# sourceMappingURL=icon-libraries.js.map