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

import { alignStyle } from './align.js';
import { borderStyle } from './border.js';
import { colorStyle } from './color.js';
import { createStyle } from './createStyle.js';
import { displayStyle } from './display.js';
import { fadeStyle } from './fade.js';
import { fillStyle, svgFillStyle } from './fill.js';
import { flowStyle } from './flow.js';
import { fontStyle } from './font.js';
import { fontStyleStyle } from './fontStyle.js';
import { gapStyle } from './gap.js';
import { groupRadiusAttr } from './groupRadius.js';
import { heightStyle } from './height.js';
import { insetStyle } from './inset.js';
import { justifyStyle } from './justify.js';
import { marginStyle } from './margin.js';
import { outlineStyle } from './outline.js';
import { paddingStyle } from './padding.js';
import { presetStyle } from './preset.js';
import { radiusStyle } from './radius.js';
import { resetStyle } from './reset.js';
import { scrollbarStyle } from './scrollbar.js';
import { shadowStyle } from './shadow.js';
import { styledScrollbarStyle } from './styledScrollbar.js';
import { transitionStyle } from './transition.js';
import { widthStyle } from './width.js';
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,
        svgFillStyle,
        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 };
}

        