/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.98.7
 * Released under the MIT license.
 */

import { alignStyle } from './align';
import { borderStyle } from './border';
import { colorStyle } from './color';
import { createStyle } from './createStyle';
import { displayStyle } from './display';
import { fadeStyle } from './fade';
import { fillStyle } from './fill';
import { flowStyle } from './flow';
import { fontStyle } from './font';
import { fontStyleStyle } from './fontStyle';
import { gapStyle } from './gap';
import { groupRadiusAttr } from './groupRadius';
import { heightStyle } from './height';
import { insetStyle } from './inset';
import { justifyStyle } from './justify';
import { marginStyle } from './margin';
import { outlineStyle } from './outline';
import { paddingStyle } from './padding';
import { presetStyle } from './preset';
import { radiusStyle } from './radius';
import { resetStyle } from './reset';
import { scrollbarStyle } from './scrollbar';
import { shadowStyle } from './shadow';
import { styledScrollbarStyle } from './styledScrollbar';
import { transitionStyle } from './transition';
import { widthStyle } from './width';
const numberConverter = (val) => {
    if (typeof val === 'number') {
        return `${val}px`;
    }
    return val;
};
const columnsConverter = (val) => {
    if (typeof val === 'number') {
        return 'minmax(1px, 1fr) '.repeat(val).trim();
    }
    return;
};
const rowsConverter = (val) => {
    if (typeof val === 'number') {
        return 'auto '.repeat(val).trim();
    }
    return;
};
export const STYLE_HANDLER_MAP = {};
export function defineCustomStyle(names, handler) {
    let handlerWithLookup;
    if (typeof names === 'function') {
        handlerWithLookup = names;
        names = handlerWithLookup.__lookupStyles;
    }
    else if (handler) {
        handlerWithLookup = Object.assign(handler, { __lookupStyles: names });
    }
    else {
        console.warn('CubeUIKit: incorrect custom style definition: ', names);
        return;
    }
    if (Array.isArray(names)) {
        // just to pass type checking
        names.forEach((name) => {
            if (!STYLE_HANDLER_MAP[name]) {
                STYLE_HANDLER_MAP[name] = [];
            }
            STYLE_HANDLER_MAP[name].push(handlerWithLookup);
        });
    }
}
export function defineStyleAlias(styleName, cssStyleName, converter) {
    const styleHandler = createStyle(styleName, cssStyleName, converter);
    if (!STYLE_HANDLER_MAP[styleName]) {
        STYLE_HANDLER_MAP[styleName] = [];
    }
    STYLE_HANDLER_MAP[styleName].push(styleHandler);
}
export function predefine() {
    // Manually define styles that are used in other custom styles.
    // Otherwise, they won't be handled as expected.
    defineStyleAlias('fontSize', 'font-size', numberConverter);
    defineStyleAlias('lineHeight', 'line-height', numberConverter);
    defineStyleAlias('fontWeight', 'font-weight', numberConverter);
    defineStyleAlias('fontStyle');
    defineStyleAlias('letterSpacing', 'letter-spacing', numberConverter);
    defineStyleAlias('textTransform');
    defineStyleAlias('fontFamily');
    defineStyleAlias('overflow');
    defineStyleAlias('paddingTop', 'padding-top', numberConverter);
    defineStyleAlias('paddingRight', 'padding-right', numberConverter);
    defineStyleAlias('paddingBottom', 'padding-bottom', numberConverter);
    defineStyleAlias('paddingLeft', 'padding-left', numberConverter);
    defineStyleAlias('marginTop', 'margin-top', numberConverter);
    defineStyleAlias('marginRight', 'margin-right', numberConverter);
    defineStyleAlias('marginBottom', 'margin-bottom', numberConverter);
    defineStyleAlias('marginLeft', 'margin-left', numberConverter);
    // Coordinates
    defineStyleAlias('top', 'top', numberConverter);
    defineStyleAlias('right', 'right', numberConverter);
    defineStyleAlias('bottom', 'bottom', numberConverter);
    defineStyleAlias('left', 'left', numberConverter);
    // Dimensions
    defineStyleAlias('minWidth', 'min-width', numberConverter);
    defineStyleAlias('maxWidth', 'max-width', numberConverter);
    defineStyleAlias('minHeight', 'min-height', numberConverter);
    defineStyleAlias('maxHeight', 'max-height', numberConverter);
    // Style aliases
    defineStyleAlias('gridAreas', 'grid-template-areas');
    defineStyleAlias('gridColumns', 'grid-template-columns', columnsConverter);
    defineStyleAlias('gridRows', 'grid-template-rows', rowsConverter);
    defineStyleAlias('gridTemplate', 'grid-template', (val) => {
        if (typeof val !== 'string')
            return;
        return val
            .split('/')
            .map((s, i) => (i ? columnsConverter : rowsConverter)(s))
            .join('/');
    });
    // Other styles
    defineStyleAlias('outlineOffset', 'outline-offset', numberConverter);
    [
        displayStyle,
        transitionStyle,
        resetStyle,
        fillStyle,
        widthStyle,
        marginStyle,
        gapStyle,
        flowStyle,
        colorStyle,
        heightStyle,
        radiusStyle,
        borderStyle,
        shadowStyle,
        paddingStyle,
        alignStyle,
        justifyStyle,
        presetStyle,
        outlineStyle,
        fontStyle,
        fontStyleStyle,
        groupRadiusAttr,
        // DEPRECATED: `styledScrollbar` is deprecated, use `scrollbar` instead
        styledScrollbarStyle,
        scrollbarStyle,
        fadeStyle,
        insetStyle,
    ]
        // @ts-ignore
        .forEach((handler) => defineCustomStyle(handler));
    return { STYLE_HANDLER_MAP, defineCustomStyle, defineStyleAlias };
}

        