'use strict';

var floatRegex = require('../utils/float-regex.js');
var isNullish = require('../utils/is-nullish.js');
var singleColorRegex = require('../utils/single-color-regex.js');

/**
 * Returns true if the provided string is a color, ie rgba(0,0,0,0) or #000,
 * but false if a number or multiple colors
 */
const isColorString = (type, testProp) => (v) => {
    return Boolean((typeof v === "string" &&
        singleColorRegex.singleColorRegex.test(v) &&
        v.startsWith(type)) ||
        (testProp &&
            !isNullish.isNullish(v) &&
            Object.prototype.hasOwnProperty.call(v, testProp)));
};
const splitColor = (aName, bName, cName) => (v) => {
    if (typeof v !== "string")
        return v;
    const [a, b, c, alpha] = v.match(floatRegex.floatRegex);
    return {
        [aName]: parseFloat(a),
        [bName]: parseFloat(b),
        [cName]: parseFloat(c),
        alpha: alpha !== undefined ? parseFloat(alpha) : 1,
    };
};

exports.isColorString = isColorString;
exports.splitColor = splitColor;
