import { isEmpty } from 'lodash';

import {
  Certificate,
  Charity,
  Discount,
  History,
  Image,
  Job,
  MediaResponse,
  Partner,
  Review,
  Select,
  Station,
  Team,
  TextResponse,
} from '../@types';

export const presentImage = (images: Image[]) =>
  isEmpty(images) ? null : `${process.env.TAYLOR_MEDIA_URL}/${images[0].url}`;

export const presentSelect = (selectItems: Select[]) =>
  !isEmpty(selectItems) ? selectItems[0].name : null;

export const presentPartners = (partners: Partner) =>
  partners.records.map((record, index) => ({
    id: index + 1,
    name: record.nazvanie,
    poster: presentImage(record.izobrozhenie),
  }));

export const presentJobs = (jobs: Job) =>
  jobs.records.map((job, index) => ({
    id: index + 1,
    name: job.zagolovok,
    tags: job.tegi.map((tag) => tag.name),
    location: presentSelect(job.lokaciya),
    type: presentSelect(job.tip),
  }));

export const presentTeamMembers = (members: Team) =>
  members.records.map((member) => ({
    name: member.polnoeImya,
    photo: presentImage(member.foto),
    profession: member.zvanie,
  }));

export const presentHistoryItems = (historyItems: History) =>
  historyItems.records.map((item) => ({
    name: item.zagolovok,
    year: item.god,
    description: item.opisanie,
  }));

export const presentDiscounts = (discounts: Discount) =>
  discounts.records.map((discount, index) => ({
    id: index + 1,
    name: discount.zagolovok,
    description: discount.opisanie,
    expiresAt: discount.do,
    image: presentImage(discount.foto),
  }));

export const presentStations = (stations: Station) =>
  stations.records.map((station, index) => ({
    id: index + 1,
    name: station.imya,
    description: station.opisanie,
    address: station.adress,
    workingHours: presentSelect(station.chasyRaboty),
    latitude: station.lat,
    longitude: station.long,
    carWash: station.avtomojka || false,
    ai92: station.ai92 || false,
    ai95: station.ai95 || false,
    dt: station.dt || false,
    z100: station.z100 || false,
    propan: station.propan || false,
    electricCharge: station.zaryadnayaStanciya || false,
    miniMarket: station.miniMarket || false,
    toilet: station.tualet || false,
    region: presentSelect(station.region),
    image: presentImage(station.foto),
  }));

export const presentTexts = (texts: TextResponse) =>
  texts.records.map((item) => ({
    key: item.klyuchNeIzmenyat,
    value: item.znachenie,
  }));

export const presentMedia = (media: MediaResponse) => {
  return media.records.map((record) => ({
    key: record.klyuchNeIzmenyat,
    name: record.mestopolozheniya,
    photo: presentImage(record.foto),
  }));
};

export const presentReviews = (reviews: Review) =>
  reviews.records.map((review) => ({
    id: review.id,
    fullname: review.polnoeImya,
    review: review.otzyv,
    rating: review.rejting,
  }));

export const presentCharities = (charities: Charity) =>
  charities.records.map((charity, index) => ({
    id: index + 1,
    name: charity.zagolovok,
    description: charity.opisanie,
    date: charity.data,
    location: charity.lokaciya,
    image: presentImage(charity.foto),
  }));

export const presentCertificates = (certificates: Certificate) =>
  certificates.records.map((certificate, index) => ({
    id: index + 1,
    name: certificate.nazvanie,
    description: certificate.opisanie,
    issuedAt: certificate.dataVydachi,
    validUntil: certificate.dejstvitelenDo,
    image: presentImage(certificate.foto),
  }));
