/**
 * @param {any} prop - description
 * @param {string} key - description
 * @param {string} value - description
 */
export const initParams = (prop, key = "key", value = "value") => {
  // get params
  const object1 = Object.fromEntries(
    prop.map(({ item, attr }) => [item[key], attr[value]])
  );
  // get mapping params
  const object2 = {};
  prop
    .filter((item) => item.attr.dataMapping)
    .map(({ attr: { dataMapping } }) => {
      for (let key in dataMapping) {
        object2[key] = null;
      }
    });
  return { ...object1, ...object2 };
};

/**
 * @param {any} prop - description
 */
export const initRules = (prop) => {
  const rules = {};
  prop
    .filter(({ item }) => item.required)
    .forEach(({ item, attr }) => {
      const message = `${item.title}不能为空`;
      rules[item.key] = attr.rules ?
        [
          { required: true, message: message, trigger: "change" },
          ...attr.rules
        ]
        : [{ required: true, message: message, trigger: "change" },];
    });
  return rules;
};

/**
 * @param {any} prop - description
 */
export const initDicts = (prop) => {
  return Array.from(
    new Set(
      prop
        .filter((item) => item.attr.dictName)
        .map((item) => item.attr.dictName)
    )
  );
};

export const initPage = () => {
  return { pageNum: 1, pageSize: 50, total: 0 };
};
