import dayjs from 'dayjs';
import _ from 'lodash';

export default {
  filesFormatter(arr) {
    if (!arr || !arr.length) return [];
    return arr.map((item) => item);
  },
  imageFormatter(arr) {
    if (!arr || !arr.length) return [];
    return arr.map((item) => ({
      publicUrl: item.publicUrl || '',
    }));
  },
  oneImageFormatter(arr) {
    if (!arr || !arr.length) return '';
    return arr[0].publicUrl || '';
  },
  dateFormatter(date) {
    if (!date) return '';
    return dayjs(date).format('YYYY-MM-DD');
  },
  dateTimeFormatter(date) {
    if (!date) return '';
    return dayjs(date).format('YYYY-MM-DD HH:mm');
  },
  booleanFormatter(val) {
    return val ? 'Yes' : 'No';
  },
  dataGridEditFormatter(obj) {
    return _.transform(obj, (result, value, key) => {
      if (_.isArray(value)) {
        result[key] = _.map(value, 'id');
      } else if (_.isObject(value)) {
        result[key] = value.id;
      } else {
        result[key] = value;
      }
    });
  },

  customersManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  customersOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  customersManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  customersOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },

  financial_accountsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.account_name);
  },
  financial_accountsOneListFormatter(val) {
    if (!val) return '';
    return val.account_name;
  },
  financial_accountsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.account_name };
    });
  },
  financial_accountsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.account_name, id: val.id };
  },

  ordersManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.total_amount);
  },
  ordersOneListFormatter(val) {
    if (!val) return '';
    return val.total_amount;
  },
  ordersManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.total_amount };
    });
  },
  ordersOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.total_amount, id: val.id };
  },

  projectsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.project_name);
  },
  projectsOneListFormatter(val) {
    if (!val) return '';
    return val.project_name;
  },
  projectsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.project_name };
    });
  },
  projectsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.project_name, id: val.id };
  },

  purchase_ordersManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.total_cost);
  },
  purchase_ordersOneListFormatter(val) {
    if (!val) return '';
    return val.total_cost;
  },
  purchase_ordersManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.total_cost };
    });
  },
  purchase_ordersOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.total_cost, id: val.id };
  },

  tasksManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.task_name);
  },
  tasksOneListFormatter(val) {
    if (!val) return '';
    return val.task_name;
  },
  tasksManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.task_name };
    });
  },
  tasksOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.task_name, id: val.id };
  },

  vendorsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.vendor_name);
  },
  vendorsOneListFormatter(val) {
    if (!val) return '';
    return val.vendor_name;
  },
  vendorsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.vendor_name };
    });
  },
  vendorsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.vendor_name, id: val.id };
  },

  rolesManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  rolesOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  rolesManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  rolesOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },

  permissionsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  permissionsOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  permissionsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  permissionsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },

  companiesManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  companiesOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  companiesManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  companiesOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },
};
