'use strict';

require('../../../utils/index.js');
var lodashEs = require('lodash-es');
var data = require('../../../utils/data.js');
var runtime = require('../../../utils/props/runtime.js');

"use strict";
const ALL_VALUE = "_ALL";
const initValue = (columns, values) => {
  const copyValues = {};
  if (columns.length) {
    columns.forEach((column) => {
      let value = lodashEs.has(values, column.key) ? values[column.key] : null;
      if (lodashEs.has(column, "options") && !column.unUseDefaultOption) {
        if (column.type === "checkbox-group" || lodashEs.has(column, "mode")) {
          value = !data.hasValue(value) || Array.isArray(value) && value.length === 0 ? [ALL_VALUE] : value;
        } else {
          value = !data.hasValue(value) ? ALL_VALUE : value;
        }
      }
      copyValues[column.key] = value;
    });
  }
  return copyValues;
};
const filterAllValue = (values, column) => {
  let value = null;
  if (Array.isArray(values)) {
    value = values.filter((item) => item !== ALL_VALUE || !data.hasValue(values));
    if (column && column.type === "number-range") {
      value = values ? values.every((e) => !lodashEs.isNull(e)) ? values : null : null;
    }
  } else {
    value = values === ALL_VALUE || !data.hasValue(values) ? null : values;
  }
  return value;
};
const treeDataFormat = (arr = [], fieldNames) => {
  let options = [];
  if (!arr) return [];
  options = arr.map((e) => {
    const obj = {};
    obj.label = e[fieldNames.label];
    obj.value = e[fieldNames.value];
    if (e[fieldNames.children] && e[fieldNames.children].length) {
      obj.children = treeDataFormat(e[fieldNames.children], fieldNames);
    }
    return obj;
  });
  return options;
};
const findLabelByValue = (tree, value) => {
  function _findLabel(nodes) {
    const labels = [];
    if (!nodes) return [];
    for (const node of nodes) {
      if (lodashEs.isEqual(node.value, value)) {
        labels.push(node.label);
      }
      if (node.children && node.children.length > 0) {
        const childLabels = _findLabel(node.children);
        if (childLabels) {
          labels.push(...childLabels);
        }
      }
    }
    return labels.length > 0 ? labels : [];
  }
  return _findLabel(tree).join(",");
};
const advancedFilteringProps = runtime.buildProps({
  filterColumns: {
    type: runtime.definePropType(Array),
    default: () => []
  },
  value: {
    type: Object,
    default: () => ({})
  },
  formOptions: {
    type: runtime.definePropType(Object),
    default: () => ({ layout: "vertical" })
  },
  overlayStyle: {
    type: runtime.definePropType(Object),
    default: () => ({
      width: "500px"
    })
  }
});
const advancedFilteringParamsProps = runtime.buildProps({
  filterColumns: {
    type: runtime.definePropType(Array),
    default: () => []
  },
  value: {
    type: Object,
    default: () => ({})
  },
  formOptions: {
    type: runtime.definePropType(Object),
    default: () => ({ layout: "vertical" })
  },
  showFooter: {
    type: Boolean,
    default: false
  }
});
const advancedFilteringEmits = {
  change: (value) => !!value,
  "update:value": (value) => !!value
};
const advancedFilteringParamsEmits = {
  close: () => true,
  confirm: () => true,
  changeValue: (value) => !!value,
  "update:value": (value) => !!value
};
const advancedFilteringTagsEmits = {
  change: (value) => !!value,
  "update:value": (value) => !!value
};

exports.ALL_VALUE = ALL_VALUE;
exports.advancedFilteringEmits = advancedFilteringEmits;
exports.advancedFilteringParamsEmits = advancedFilteringParamsEmits;
exports.advancedFilteringParamsProps = advancedFilteringParamsProps;
exports.advancedFilteringProps = advancedFilteringProps;
exports.advancedFilteringTagsEmits = advancedFilteringTagsEmits;
exports.filterAllValue = filterAllValue;
exports.findLabelByValue = findLabelByValue;
exports.initValue = initValue;
exports.treeDataFormat = treeDataFormat;
//# sourceMappingURL=advanced-filtering.js.map
