"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.recipeRunner = exports.whenIsTrue = exports.stringToTsType = exports.deepClone = exports.mergeDeep = exports.pick = exports.omit = exports.isObject = exports.capitalize = exports.uncapitalize = void 0;
var immer_1 = __importDefault(require("immer"));
function uncapitalize(string) {
    return string[0].toLowerCase() + string.slice(1);
}
exports.uncapitalize = uncapitalize;
function capitalize(string) {
    return string[0].toUpperCase() + string.slice(1);
}
exports.capitalize = capitalize;
function isObject(item) {
    return item && typeof item === 'object' && !Array.isArray(item);
}
exports.isObject = isObject;
function omit(obj, fields) {
    // eslint-disable-next-line prefer-object-spread
    var shallowCopy = Object.assign({}, obj);
    for (var i = 0; i < fields.length; i += 1) {
        var key = fields[i];
        delete shallowCopy[key];
    }
    return shallowCopy;
}
exports.omit = omit;
function pick(obj, fields) {
    // eslint-disable-next-line prefer-object-spread
    var shallowCopy = {};
    for (var i = 0; i < fields.length; i += 1) {
        var key = fields[i];
        if (obj.hasOwnProperty(key)) {
            shallowCopy[key] = obj[key];
        }
    }
    return shallowCopy;
}
exports.pick = pick;
function mergeDeep(target, source) {
    var output = Object.assign({}, target);
    if (isObject(target) && isObject(source)) {
        Object.keys(source).forEach(function (key) {
            var _a, _b;
            if (isObject(source[key])) {
                if (!(key in target))
                    Object.assign(output, (_a = {}, _a[key] = source[key], _a));
                else
                    output[key] = mergeDeep(target[key], source[key]);
            }
            else {
                Object.assign(output, (_b = {}, _b[key] = source[key], _b));
            }
        });
    }
    return output;
}
exports.mergeDeep = mergeDeep;
function deepClone(obj) {
    var newObj = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === 'object') {
        Object.entries(obj).forEach(function (_a) {
            var key = _a[0], value = _a[1];
            newObj[key] = typeof value === 'object' ? deepClone(value) : value;
        });
    }
    return newObj;
}
exports.deepClone = deepClone;
var stringToTsType = function (value, dataTypeString) {
    if (typeof value !== 'string') {
        return value;
    }
    switch (dataTypeString) {
        case 'string':
            break;
        case 'boolean':
            if (value === 'true') {
                return true;
            }
            return false;
        case 'bigint':
        case 'number':
            return Number(value);
        default:
            break;
    }
    return value;
};
exports.stringToTsType = stringToTsType;
function whenIsTrue(expact, realValues) {
    if (!expact) {
        return true;
    }
    if (expact === '*') {
        return true;
    }
    if (typeof expact === 'function') {
        return expact(realValues);
    }
    return Object.entries(expact).every(function (_a) {
        var k = _a[0], v = _a[1];
        if (k === 'id') {
            if (realValues[k] === undefined) {
                // 新增模式
                return undefined === v;
            }
            return '*' === v; //编辑模式
        }
        return Array.isArray(v) ? v.includes(realValues[k]) : realValues[k] === v;
    });
}
exports.whenIsTrue = whenIsTrue;
function recipeRunner(recipe, state) {
    if (!recipe) {
        return state;
    }
    return (0, immer_1.default)(state, recipe);
}
exports.recipeRunner = recipeRunner;
//# sourceMappingURL=common-helpers.js.map