// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import * as PuppeteerReplay from '../../../third_party/puppeteer-replay/puppeteer-replay.js';

import {
  type EmulateNetworkConditionsStep,
  type SetViewportStep,
  type Step,
  StepType,
} from './Schema.js';

export function createViewportStep(viewport: {
  clientWidth: number,
  clientHeight: number,
}): SetViewportStep {
  return {
    type: StepType.SetViewport,
    width: viewport.clientWidth,
    height: viewport.clientHeight,
    // TODO read real parameters here
    deviceScaleFactor: 1,
    isMobile: false,
    hasTouch: false,
    isLandscape: false,
  };
}

export function createEmulateNetworkConditionsStep(conditions: {
  download: number,
  upload: number,
  latency: number,
}): EmulateNetworkConditionsStep {
  return {
    type: StepType.EmulateNetworkConditions,
    download: conditions.download,
    upload: conditions.upload,
    latency: conditions.latency,
  };
}

export function areSelectorsEqual(stepA: Step, stepB: Step): boolean {
  if ('selectors' in stepA && 'selectors' in stepB) {
    return JSON.stringify(stepA.selectors) === JSON.stringify(stepB.selectors);
  }
  return !('selectors' in stepA) && !('selectors' in stepB);
}

export const minTimeout = 1;
export const maxTimeout = 30000;
export const parse = PuppeteerReplay.parse;
export const parseStep = PuppeteerReplay.parseStep;
