"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toCssText = exports.toRPX = exports.toREM = exports.setDistanceStyle = exports.setStyleValue = exports.translateStyleByHandler = exports.translateStyleToRem = exports.translateStyleToRpx = exports._handleStyleNumValue = void 0;
var common_1 = require("./common");
var lodash_es_1 = require("lodash-es");
var PERCENTAGE_KEY_LIST = [
    'opacity',
    'order',
    'flex',
    'flexGrow',
    'flexShrink',
    'zIndex',
    'fontWeight',
    'borderImage',
];
var DISTANCE_KEY_LIST = ['top', 'right', 'bottom', 'left'];
function _handleStyleNumValue(styleVal, addPXUnit) {
    if (addPXUnit) {
        var value = "" + styleVal;
        if (value.search(/^\d+$/) >= 0) {
            return value + 'px';
        }
    }
    return styleVal;
}
exports._handleStyleNumValue = _handleStyleNumValue;
function translateStyleToRpx(style) {
    if (style === void 0) { style = {}; }
    return translateStyleByHandler(style, toRPX);
}
exports.translateStyleToRpx = translateStyleToRpx;
function translateStyleToRem(style) {
    return translateStyleByHandler(style, toREM);
}
exports.translateStyleToRem = translateStyleToRem;
function translateStyleByHandler(style, handler) {
    return Object.keys(style).reduce(function (result, key) {
        var value = style[key];
        if (PERCENTAGE_KEY_LIST.includes(key)) {
            setStyleValue(result, key, value);
        }
        else if (value !== undefined && value !== null) {
            setStyleValue(result, key, handler(value));
        }
        return result;
    }, {});
}
exports.translateStyleByHandler = translateStyleByHandler;
function setStyleValue(object, key, value) {
    if (value === undefined || value === null || value === '') {
        return;
    }
    // 特殊样式移除
    if (key === 'open') {
        return;
    }
    if ((0, common_1.isEmptyObj)(value)) {
        return;
    }
    object[(0, common_1.camelcase)(key)] = value;
}
exports.setStyleValue = setStyleValue;
function setDistanceStyle(style, distance, attr, addPXUnit) {
    if (Object.keys(distance).length === 4) {
        if ((0, lodash_es_1.uniq)((0, lodash_es_1.map)(distance, function (val) { return val; })).length === 1) {
            // 4个值全相等的情况
            setStyleValue(style, (0, common_1.camelcase)("" + attr), _handleStyleNumValue(distance.top, addPXUnit));
        }
        else if (distance.top === distance.bottom &&
            distance.left === distance.right) {
            // 俩俩相等的情况
            setStyleValue(style, (0, common_1.camelcase)("" + attr), _handleStyleNumValue(distance.top, addPXUnit) + " " + _handleStyleNumValue(distance.right, addPXUnit));
        }
        else if (distance.left === distance.right) {
            setStyleValue(style, (0, common_1.camelcase)("" + attr), _handleStyleNumValue(distance.top, addPXUnit) + " " + _handleStyleNumValue(distance.right, addPXUnit) + " " + _handleStyleNumValue(distance.bottom, addPXUnit));
        }
        else {
            setStyleValue(style, (0, common_1.camelcase)("" + attr), _handleStyleNumValue(distance.top, addPXUnit) + " " + _handleStyleNumValue(distance.right, addPXUnit) + " " + _handleStyleNumValue(distance.bottom, addPXUnit) + " " + _handleStyleNumValue(distance.left, addPXUnit));
        }
    }
    else {
        DISTANCE_KEY_LIST.forEach(function (key) {
            if (distance[key] !== undefined)
                setStyleValue(style, (0, common_1.camelcase)(attr + "_" + key), _handleStyleNumValue(distance[key], addPXUnit));
        });
    }
}
exports.setDistanceStyle = setDistanceStyle;
function calPxToREM(px) {
    if (Number.isNaN(px / 28))
        return px.toString();
    if (+px === 0) {
        return '0';
    }
    return (px / 28).toFixed(4) + 'rem';
}
function toREM(cssLen) {
    if (typeof cssLen === 'string') {
        var cssLenArr = cssLen.split(' ');
        return cssLenArr
            .map(function (attr) {
            var matchResult = attr.match(/^(-?\d+)(px)?$/);
            if (matchResult && matchResult[1]) {
                return calPxToREM(+matchResult[1]);
            }
            return attr;
        })
            .join(' ');
    }
    if (typeof cssLen === 'number') {
        return calPxToREM(cssLen);
    }
}
exports.toREM = toREM;
function toRPX(cssLen) {
    if (typeof cssLen === 'string') {
        var cssLenArr = cssLen.split(' ');
        return cssLenArr
            .map(function (attr) {
            var matchResult = attr.match(/^(-?\d+)(px)?$/);
            if (matchResult && matchResult[1]) {
                return +matchResult[1] + "rpx";
            }
            return attr;
        })
            .join(' ');
    }
    return cssLen + "rpx";
}
exports.toRPX = toRPX;
function toCssText(style, className) {
    if (className === void 0) { className = '.some-class-name'; }
    var attrText = Object.keys(style)
        .map(function (key) {
        var value = style[key];
        return (0, common_1.kebabCase)(key) + ": " + value + ";";
    })
        .join('\n');
    return className + " { " + attrText + " }\n";
}
exports.toCssText = toCssText;
//# sourceMappingURL=style.js.map