<script lang="ts">

    export type Attachment = {
        url: string;
    }
    export type WorkoutLinkItem = {
      type: string;
      value: string;
    }
    export type WorkoutLink = {
      values: Array<WorkoutLinkItem>;
    }
    export type WorkoutItem = {
        id: string;
        name: string;
        created_by: string;
        created_at: string;
        updated_at: string;
        description: string;
        cadence: number;
        heart_rate: number;
        max_cadence: number;
        max_heart_rate: number;
        temperature: number;
        speed: number;
        power: number;
        max_speed: number;
        max_power: number;
        duraion_sec: number;
        distantion: number;
        attachment: Attachment;
        latitude: number;
        longitude: number;
        is_public: boolean;
        workouted_at: string;
        external_links?: WorkoutLink;
    }
    export const secondsToDuration = (seconds: number) =>  {
        let hours = Math.floor(seconds / 3600);
        let minutes = Math.floor((seconds % 3600) / 60);
        return `${hours} ч. ${minutes} мин.`;
    }

    export const formatTime = (isoString: string): string => {
      const date = new Date(isoString);
      if (Number.isNaN(date.valueOf())) {
        throw new Error('Invalid date string');
      }
      const day = date.getDate();
      const month = date.getMonth() + 1; // Months are 0-based
      const year = date.getFullYear();
      return `${pad(day)}.${pad(month)}.${year}`;
    }

    const pad = (n: number): string => {
      return `${Math.floor(Math.abs(n))}`.padStart(2, '0');
    }
    
    export const distConvert = (speed: number) => {
      return Math.round(speed/1000)
    }

    export const speedConvert = (speed: number) => {
      return Math.round(speed*3.6)
    }
    export type ChartDataset = {
        "radius": number,
        "label": string,
        "backgroundColor": string,
        "borderColor": string,
        "data": Array<number>,
    }
    export type ChartData = {
        "labels": Array<string>,
        "datasets": Array<ChartDataset>,
    }
    export default {}
</script>