<commit_before># frozen_string_literal: true

module Toggl
  module Worktime
    # Time-entries merger
    class Merger
      def initialize(time_entries)
        @time_entries = time_entries
        @current_start = nil
        @current_stop = nil
        @continuing = true
        @last_stop = nil
      end

      def merge
        work_time = []
        time_entries_each do |start, stop|
          if continuing(start)
            @current_stop = stop
            next
          end
          work_time << [@current_start, @current_stop]
          @current_start = start
          @current_stop = nil
        end
        work_time << [@current_start, @last_stop]
        work_time
      end

      def time_entries_each
        zone_offset = Toggl::Worktime::Time.zone_offset(DEFAULT_TIMEZONE)
        @time_entries.each do |te|
          start = DateTime.parse(te['start']).new_offset(zone_offset)
          stop = DateTime.parse(te['stop']).new_offset(zone_offset)
          @last_stop = stop
          @current_start = start if @current_start.nil?
          @current_stop = stop if @current_stop.nil?
          yield [start, stop]
        end
      end

      def continuing(start)
        interval = (start - @current_stop) * ONE_DAY_MINUTES<commit_msg>Date parser handles nil<commit_after># frozen_string_literal: true

module Toggl
  module Worktime
    # Time-entries merger
    class Merger
      def initialize(time_entries)
        @time_entries = time_entries
        @current_start = nil
        @current_stop = nil
        @continuing = true
        @last_stop = nil
      end

      def merge
        work_time = []
        time_entries_each do |start, stop|
          if continuing(start)
            @current_stop = stop
            next
          end
          work_time << [@current_start, @current_stop]
          @current_start = start
          @current_stop = nil
        end
        work_time << [@current_start, @last_stop]
        work_time
      end

      def time_entries_each
        zone_offset = Toggl::Worktime::Time.zone_offset(DEFAULT_TIMEZONE)
        @time_entries.each do |te|
          start = parse_date(te['start'], zone_offset)
          stop = parse_date(te['stop'], zone_offset)
          @last_stop = stop
          @current_start = start if @current_start.nil?
          @current_stop = stop if @current_stop.nil?
          yield [start, stop]
        end
      end

      def parse_date(date, zone_offset)
        return nil if date.nil?
        DateTime.parse(date).new_offset(zone_offset)
      end

      def continuing(start)
        interval = (start - @current_stop) * ONE_DAY_MINUTES
{'fraction_non_alphanumeric': 0.08062074186222559, 'fraction_numerical': 0.0, 'mean_word_length': 2.027491408934708, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1334555', 'n_tokens_mistral': 776, 'n_tokens_neox': 762, 'n_words': 177}