function updateStrongMiddleWithPeriods() {
  const PLAYED_COUNT = { min: 2, max: 100 }
  const FULL_DIFF = { min: 70, max: 112 }
  const SKIP_DIFF = { min: 0, max: 51 }
  const SKIP_THRESHOLD = 12
  const SKIP_DAYS_REL = 20 // skipRecentDays чем больше тем меньше  треков
//  const MIN_CROSS_COUNT = 3
  const MIN_CROSS_COUNT = 0

  const PERIODS = [
    { period: 'night', from: 0, to: 5 },
    { period: 'morning', from: 6, to: 11 },
    { period: 'day', from: 12, to: 17 },
    { period: 'evening', from: 18, to: 23 },
  ];

  let targetPeriod = '';
/** */
  Clerk.runOnceAfter('00:00', function nightStrongPls() { onSetDayPeriod() });
  Clerk.runOnceAfter('06:00', function morningStrongPls() { onSetDayPeriod() });
  Clerk.runOnceAfter('12:00', function dayStrongPls() { onSetDayPeriod() });
  Clerk.runOnceAfter('18:00', function eveningStrongPls() { onSetDayPeriod() });
  if (targetPeriod.length == 0) {
    return;
  }
/***/
  let tracks = getRecentTracks()
  console.log(`Tracks with crossPeriodCount >= ${MIN_CROSS_COUNT}: ${tracks.filter(t => t.crossPeriodCount >= MIN_CROSS_COUNT).length}`);
  calcDiff()
  skipRecentDays()
  reduceDiff()
  filter()

  Playlist.saveWithReplace({
    name: 'Претенденты на лайки',
    id: '74GhYFqkRfa01cM0YDLvDk',
    tracks: Selector.sliceRandom(tracks, 30)
  })

  function getRecentTracks() {
    let recentTracks = Combiner.push([], Cache.read('SpotifyRecentTracks'), Cache.read('LastfmRecentTracks'))

    let grouped = Object.values(recentTracks.reduce((acc, track) => {
      acc[track.id] = acc[track.id] || []
      acc[track.id].push(track)
      return acc
    }, {}))

    let deduped = grouped.map(tracks => {
      Order.sort(tracks, 'meta.played_at', 'desc')
      for (let i = 1; i < tracks.length; i++) {
        let xTime = new Date(tracks[i - 1])
        let yTime = new Date(tracks[i])
        if (xTime - yTime <= 10000) {
          i--
          tracks.splice(i, 1)
        }
      }
      return tracks
    }).flat(1)

    Order.sort(deduped, 'meta.played_at', 'asc')
    console.log(`Tracks recieved: ${deduped.length} `);

    return deduped
  }

  function calcDiff() {
    for (let i = 1; i < tracks.length; i++) {
      let aEnd = new Date(tracks[i - 1].played_at).getTime()
      let bEnd = new Date(tracks[i].played_at).getTime()
      let delta = bEnd - aEnd
      tracks[i].diffProcent = 100 * delta / tracks[i].duration_ms
    }
    console.log(`Tracks after calcDiff: ${tracks.length} `);

  }

  function skipRecentDays() {
    let recentTracks = RecentTracks.get()
    Filter.rangeDateRel(recentTracks, SKIP_DAYS_REL, 0)
  
    // Оставляем некоторое количество недавних треков
    let tracksToKeep = Math.floor(recentTracks.length * 0.2) // например, 20%
    recentTracks = recentTracks.slice(0, -tracksToKeep)

    Filter.removeTracks(tracks, recentTracks)
    console.log(`Tracks after skipRecentDays: ${tracks.length} `);
  }

  function reduceDiff() {
    tracks = Object.values(tracks.reduce((acc, track) => {
      acc[track.id] = acc[track.id] || track

      acc[track.id].diffGroup = acc[track.id].diffGroup || []
      acc[track.id].diffGroup.push(track.diffProcent)

      acc[track.id].periods = acc[track.id].periods || []
      acc[track.id].periods.push(track.played_at || track.added_at)
      return acc
    }, {})).map((t, i) => {
      t.crossPeriodCount = t.periods.filter(p => isDayPeriod(new Date(p), targetPeriod)).length
      return t
    })
    console.log(`Tracks after reduceDiff: ${tracks.length} `);

  }

  function filter() {
    tracks = tracks.filter(t => {
      t.fullPlayedCount = reduce(t, FULL_DIFF.min, FULL_DIFF.max)
      t.skipCount = reduce(t, SKIP_DIFF.min, SKIP_DIFF.max)
      return t.skipCount <= SKIP_THRESHOLD && t.crossPeriodCount >= MIN_CROSS_COUNT
        && (t.fullPlayedCount >= PLAYED_COUNT.min && t.fullPlayedCount <= PLAYED_COUNT.max)
    })

    function reduce(track, min, max) {
      return track.diffGroup.reduce((acc, value) => {
        let int = Math.round(value)
        return int >= min && int <= max ? ++acc : acc
      }, 0)
    }
    console.log(`Tracks after filter: ${tracks.length} `);
   
  }