import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_deepmerge__ from "deepmerge";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
function getEnumerableOwnPropertySymbols(target) {
    return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter((symbol)=>target.propertyIsEnumerable(symbol)) : [];
}
function getKeys(target) {
    return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target));
}
const mergeStrategies = new Map();
mergeStrategies.set('overwrite', (_, y)=>y);
mergeStrategies.set('andMerge', (x, y)=>{
    if (!x && !y) return;
    if (!x) return y;
    if (!y) return x;
    return {
        $and: [
            x,
            y
        ]
    };
});
mergeStrategies.set('orMerge', (x, y)=>{
    if (!x && !y) return;
    if (!x) return y;
    if (!y) return x;
    return {
        $or: [
            x,
            y
        ]
    };
});
mergeStrategies.set('deepMerge', (x, y)=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.isPlainObject)(x) && (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.isPlainObject)(y) ? (0, __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"])(x, y, {
        arrayMerge: (x, y)=>y
    }) : y);
mergeStrategies.set('merge', (x, y)=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.isPlainObject)(x) && (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.isPlainObject)(y) ? Object.assign(x, y) : y);
mergeStrategies.set('union', (x, y)=>{
    if ('string' == typeof x) x = x.split(',');
    if ('string' == typeof y) y = y.split(',');
    return (0, __WEBPACK_EXTERNAL_MODULE_lodash__.uniq)((x || []).concat(y || []));
});
mergeStrategies.set('intersect', (x, y)=>{
    if ('string' == typeof x) x = x.split(',');
    if ('string' == typeof y) y = y.split(',');
    if (!Array.isArray(x) || 0 === x.length) return y || [];
    if (!Array.isArray(y) || 0 === y.length) return x || [];
    return x.filter((v)=>y.includes(v));
});
function assign_rslib_entry_assign(target, source) {
    let strategies = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {};
    getKeys(source).forEach((sourceKey)=>{
        const strategy = strategies[sourceKey];
        let func = mergeStrategies.get('deepMerge');
        if ('function' == typeof strategy) func = strategy;
        else if ('string' == typeof strategy && mergeStrategies.has(strategy)) func = mergeStrategies.get(strategy);
        target[sourceKey] = func(target[sourceKey], source[sourceKey]);
    });
    return target;
}
export { assign_rslib_entry_assign as assign, mergeStrategies };
