function clampHeight(viewportHeight) {
  const numericHeight = Number(viewportHeight)
  if (!Number.isFinite(numericHeight) || numericHeight <= 0) {
    return 720
  }

  return Math.max(520, numericHeight - 220)
}

export function buildOnlyOfficePreviewConfig(config, options = {}) {
  const viewportHeight = options.viewportHeight
  const editorConfig = {
    ...(config.editorConfig || {}),
    embedded: {
      embedUrl: '',
      fullscreenUrl: '',
      saveUrl: '',
      shareUrl: '',
      toolbarDocked: 'top'
    }
  }

  return {
    ...config,
    type: 'embedded',
    editorConfig,
    width: '100%',
    height: `${clampHeight(viewportHeight)}px`
  }
}

export function buildOnlyOfficeEditorConfig(config, options = {}) {
  const viewportHeight = options.viewportHeight
  const editable = Boolean(options.editable)
  const fillContainer = Boolean(options.fillContainer)

  return {
    ...config,
    type: editable ? 'desktop' : 'embedded',
    editorConfig: {
      ...(config.editorConfig || {}),
      embedded: editable
        ? undefined
        : {
            embedUrl: '',
            fullscreenUrl: '',
            saveUrl: '',
            shareUrl: '',
            toolbarDocked: 'top'
          }
    },
    width: '100%',
    height: fillContainer ? '100%' : `${clampHeight(viewportHeight)}px`
  }
}
