import { computed, inject, ref } from 'vue';

export const useTable = (props) => {
  /**
   * table配置
   */
  const table = computed(() => {
    // Options des collonnes par défaut
    const defaultColumnOptions = {
      align: 'left',
      sortable: true,
      type: 'string',
    };

    // Option de la table par défaut
    const defautOptionsTable = {
      loading: false,
      // check tous les champs déclaré si une modification est détecté on redessine la row
      'row-key': (row) =>
        config.columns
          .filter((c) => c.name !== undefined || c.field !== undefined)
          .map((c) => row[c.name ? c.name : c.field])
          .join(),
    };

    let config = props.table ? props.table : {};

    // Application des options par défaut sur la table
    config = { ...defautOptionsTable, ...config };

    // Application des options par défaut sur les clés spéficiques
    Object.keys(config).forEach((key) => {
      if (key === 'columns') {
        config[key] = config[key].map((conf) => {
          return { ...defaultColumnOptions, ...conf };
        });
      }
    });

    return config;
  });

  return {
    table,
  };
};
