import { provideStaticPackageSource } from '@anglr/dynamic';
import { isBlank, isBoolean, isJsObject, isPresent, isString, isType } from '@jscrpt/common';
import { DEFAULT_RELATIONS_MODULE_TYPES_EXTRACTOR, DEFAULT_RELATIONS_NODES_EXTRACTOR, RELATIONS_MODULE_TYPES_LOADER_PROVIDER, RELATIONS_NODES_LOADER_PROVIDER, COMPONENTS_RELATIONS_NODES_EXTRACTOR, STATIC_COMPONENTS_RELATIONS_NODES_PROVIDER, STATIC_COMPONENTS_RELATIONS_MODULE_TYPES_PROVIDER, RELATIONS_HISTORY_MANAGER_STATE, RELATIONS_HISTORY_MANAGER_PROVIDER } from './providers';
import { RelationsNodeManager, ScopeRegister, StaticComponentsRegister } from '../services';
import { RELATIONS_DEFAULTS_OVERRIDE } from './tokens';
/**
 * Clamps number between two values
 * @param num - Number to clamp
 * @param min - Minimum value
 * @param max - Maximum value
 */
export function clamp(num, min, max) {
    return Math.min(Math.max(num, min), max);
}
/**
 * Checks whether data is relations module types
 * @param data - Data to be checked
 */
export const isRelationsModuleTypes = function (data) {
    if (isBlank(data === null || data === void 0 ? void 0 : data.data) || !Array.isArray(data.data)) {
        return false;
    }
    return true;
};
/**
 * Checks whether data is relations node def
 * @param data - Data to be checked
 */
export const isRelationsNodeDef = function (data) {
    //type is required and must be type
    if (isBlank(data === null || data === void 0 ? void 0 : data.data) || !isType(data.data)) {
        return false;
    }
    //singleton should be boolean if used
    if (isPresent(data.singleton) && !isBoolean(data.singleton)) {
        return false;
    }
    //display name should be string if used
    if (isPresent(data.displayName) && !isString(data.displayName)) {
        return false;
    }
    //meta info should be object if used
    if (isPresent(data === null || data === void 0 ? void 0 : data.metaInfo) && !isJsObject(data.metaInfo)) {
        return false;
    }
    return true;
};
/**
 * Default providers for relations editor subpackage
 */
export function provideRelationsEditor() {
    return [
        DEFAULT_RELATIONS_NODES_EXTRACTOR,
        COMPONENTS_RELATIONS_NODES_EXTRACTOR,
        DEFAULT_RELATIONS_MODULE_TYPES_EXTRACTOR,
        RELATIONS_MODULE_TYPES_LOADER_PROVIDER,
        RELATIONS_NODES_LOADER_PROVIDER,
        RelationsNodeManager,
        ScopeRegister,
        RELATIONS_HISTORY_MANAGER_STATE,
        RELATIONS_HISTORY_MANAGER_PROVIDER,
    ];
}
/**
 * Providers for relations editor subpackage, with support of static components
 * @param staticRegister - Type that represents implementation of static components register
 */
export function provideRelationsEditorWithStatic(staticRegister) {
    return [
        ...provideRelationsEditor(),
        STATIC_COMPONENTS_RELATIONS_NODES_PROVIDER,
        STATIC_COMPONENTS_RELATIONS_MODULE_TYPES_PROVIDER,
        {
            provide: StaticComponentsRegister,
            useClass: staticRegister
        },
        provideStaticPackageSource('static-components'),
    ];
}
/**
 * Provider for relations defaults override
 * @param defaultsOverride - Service to pride as relations defaults override
 */
export function provideRelationsDefaultOverride(defaultsOverride) {
    return {
        provide: RELATIONS_DEFAULTS_OVERRIDE,
        useClass: defaultsOverride
    };
}
/**
 * Transforms any string to colour code
 * @param str - String to be changed to colour code
 */
export function stringToColour(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }
    let colour = '#';
    for (let i = 0; i < 3; i++) {
        const value = (hash >> (i * 8)) & 0xFF;
        colour += ('00' + value.toString(16)).substr(-2);
    }
    return colour;
}
//# sourceMappingURL=utils.js.map