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;
      }
    });
  },

  clientsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.first_name);
  },
  clientsOneListFormatter(val) {
    if (!val) return '';
    return val.first_name;
  },
  clientsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.first_name };
    });
  },
  clientsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.first_name, id: val.id };
  },

  plansManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  plansOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  plansManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  plansOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },

  reservationsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.ref);
  },
  reservationsOneListFormatter(val) {
    if (!val) return '';
    return val.ref;
  },
  reservationsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.ref };
    });
  },
  reservationsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.ref, id: val.id };
  },

  subscriptionsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.provider_customer_id);
  },
  subscriptionsOneListFormatter(val) {
    if (!val) return '';
    return val.provider_customer_id;
  },
  subscriptionsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.provider_customer_id };
    });
  },
  subscriptionsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.provider_customer_id, id: val.id };
  },

  tenantsManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  tenantsOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  tenantsManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  tenantsOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },

  vehiclesManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.plate_number);
  },
  vehiclesOneListFormatter(val) {
    if (!val) return '';
    return val.plate_number;
  },
  vehiclesManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.plate_number };
    });
  },
  vehiclesOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.plate_number, 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 };
  },

  agencesManyListFormatter(val) {
    if (!val || !val.length) return [];
    return val.map((item) => item.name);
  },
  agencesOneListFormatter(val) {
    if (!val) return '';
    return val.name;
  },
  agencesManyListFormatterEdit(val) {
    if (!val || !val.length) return [];
    return val.map((item) => {
      return { id: item.id, label: item.name };
    });
  },
  agencesOneListFormatterEdit(val) {
    if (!val) return '';
    return { label: val.name, id: val.id };
  },
};
