"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getComponentValidator = exports.getBooleanValidator = exports.getNumberArrayValidator = exports.getFunctionValidator = exports.getStringOrGradientValidator = exports.getStringValidator = exports.getNumberOrAutoValidator = exports.getNumberOrArrayOfNumbersValidator = exports.getNumberValidator = exports.alphaComponent = exports.RGBComponent = void 0; const Global_1 = require("./Global"); const Util_1 = require("./Util"); function _formatValue(val) { if (Util_1.Util._isString(val)) { return '"' + val + '"'; } if (Object.prototype.toString.call(val) === '[object Number]') { return val; } if (Util_1.Util._isBoolean(val)) { return val; } return Object.prototype.toString.call(val); } function RGBComponent(val) { if (val > 255) { return 255; } else if (val < 0) { return 0; } return Math.round(val); } exports.RGBComponent = RGBComponent; function alphaComponent(val) { if (val > 1) { return 1; } else if (val < 0.0001) { return 0.0001; } return val; } exports.alphaComponent = alphaComponent; function getNumberValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { if (!Util_1.Util._isNumber(val)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a number.'); } return val; }; } } exports.getNumberValidator = getNumberValidator; function getNumberOrArrayOfNumbersValidator(noOfElements) { if (Global_1.Konva.isUnminified) { return function (val, attr) { let isNumber = Util_1.Util._isNumber(val); let isValidArray = Util_1.Util._isArray(val) && val.length == noOfElements; if (!isNumber && !isValidArray) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a number or Array(' + noOfElements + ')'); } return val; }; } } exports.getNumberOrArrayOfNumbersValidator = getNumberOrArrayOfNumbersValidator; function getNumberOrAutoValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { var isNumber = Util_1.Util._isNumber(val); var isAuto = val === 'auto'; if (!(isNumber || isAuto)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a number or "auto".'); } return val; }; } } exports.getNumberOrAutoValidator = getNumberOrAutoValidator; function getStringValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { if (!Util_1.Util._isString(val)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a string.'); } return val; }; } } exports.getStringValidator = getStringValidator; function getStringOrGradientValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { const isString = Util_1.Util._isString(val); const isGradient = Object.prototype.toString.call(val) === '[object CanvasGradient]' || (val && val.addColorStop); if (!(isString || isGradient)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a string or a native gradient.'); } return val; }; } } exports.getStringOrGradientValidator = getStringOrGradientValidator; function getFunctionValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { if (!Util_1.Util._isFunction(val)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a function.'); } return val; }; } } exports.getFunctionValidator = getFunctionValidator; function getNumberArrayValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { const TypedArray = Int8Array ? Object.getPrototypeOf(Int8Array) : null; if (TypedArray && val instanceof TypedArray) { return val; } if (!Util_1.Util._isArray(val)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a array of numbers.'); } else { val.forEach(function (item) { if (!Util_1.Util._isNumber(item)) { Util_1.Util.warn('"' + attr + '" attribute has non numeric element ' + item + '. Make sure that all elements are numbers.'); } }); } return val; }; } } exports.getNumberArrayValidator = getNumberArrayValidator; function getBooleanValidator() { if (Global_1.Konva.isUnminified) { return function (val, attr) { var isBool = val === true || val === false; if (!isBool) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be a boolean.'); } return val; }; } } exports.getBooleanValidator = getBooleanValidator; function getComponentValidator(components) { if (Global_1.Konva.isUnminified) { return function (val, attr) { if (val === undefined || val === null) { return val; } if (!Util_1.Util.isObject(val)) { Util_1.Util.warn(_formatValue(val) + ' is a not valid value for "' + attr + '" attribute. The value should be an object with properties ' + components); } return val; }; } } exports.getComponentValidator = getComponentValidator;