"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const R = require('ramda');
let id = 0;
function isEmpty(i) {
    if (typeof i === 'string') {
        return !i.trim();
    }
    return R.isEmpty(i) || R.isNil(i) || Number.isNaN(i);
}
function px(p) {
    return `${p}px`;
}
function pxWithAuto(p) {
    return typeof p === 'number' ? `${p}px` : 'auto';
}
const Tools = {
    getAssetCdn(domain, key) {
        domain = domain || '//9.url.cn/edu/cms';
        key = key || 'edu';
        return [domain, key].join('/');
    },
    getPosition(commonStyle) {
        const { position } = commonStyle;
        if (typeof position !== 'object') {
            return null;
        }
        const style = {
            position: 'fixed',
        };
        const { outset, top, left, bottom, right } = position;
        switch (outset) {
            case 'TopLeft': {
                style.top = top;
                style.left = left;
                break;
            }
            case 'TopRight': {
                style.top = top;
                style.right = right;
                break;
            }
            case 'BottomRight': {
                style.bottom = bottom;
                style.right = right;
                break;
            }
            case 'BottomLeft': {
                style.bottom = bottom;
                style.left = left;
                break;
            }
            default:
                break;
        }
        return style;
    },
    mergeCommonStyle(commonStyle) {
        const { size, transform, text, border, background, margin, padding, zIndex } = commonStyle;
        const style = {};
        if (size) {
            if (!size.autoWidth) {
                style.width = size.width;
            }
            else {
                style.maxWidth = '100%';
            }
            if (!size.autoHeight) {
                style.height = size.height;
            }
        }
        if (transform) {
            const { rotate, opacity, scale, radius } = transform;
            if ((rotate && rotate !== 0) || (scale && scale !== 1)) {
                style.transform = `${rotate ? `rotate(${rotate}deg) ` : ''}${scale ? `scale(${scale})` : ''}`;
            }
            if (opacity && opacity < 1) {
                style.opacity = opacity;
            }
            if (radius && radius > 0) {
                style.borderRadius = `${radius}px`;
            }
        }
        if (margin) {
            const { top, right, bottom, left } = margin;
            if (top !== 0 || right !== 0 || bottom !== 0 || left !== 0) {
                style.margin = [top, right, bottom, left].map(pxWithAuto).join(' ');
            }
        }
        if (padding) {
            const { top, right, bottom, left } = padding;
            if (top !== 0 || right !== 0 || bottom !== 0 || left !== 0) {
                style.padding = [top, right, bottom, left].map(px).join(' ');
            }
        }
        if (typeof zIndex === 'number') {
            style.zIndex = zIndex;
        }
        if (text) {
            style.color = text.color;
            style.fontSize = text.fontSize;
            style.lineHeight = `${text.lineHeight}px`;
            style.textAlign = text.textAlign;
            style.fontWeight = text.weight;
        }
        if (border) {
            const { type, color, width } = border;
            style.border = `${width}px ${type} ${color}`;
        }
        if (background) {
            const { color, image } = background;
            style.background = isEmpty(image) ? color : `url(${image})`;
        }
        return style;
    },
    generateId() {
        return id++;
    },
};
exports.default = Tools;
//# sourceMappingURL=tools.js.map