import * as React from 'react'
import {Box, rem, Text} from '@sanity/ui'
import styled from 'styled-components'
import {FromToIndex, Annotation, FieldChangeNode} from '../../types'
import {getAnnotationAtPath} from '../annotations'
import {DiffCard} from './DiffCard'
const RoundedCard = styled.div`
border-radius: ${({theme}) => rem(theme.sanity.radius[2])};
padding: ${({theme}) => rem(theme.sanity.space[1])};
`
const AnnotationText = styled(Text)`
&:not([hidden]) {
color: inherit;
}
`
export function ChangeTitleSegment({
change,
segment,
}: {
change?: FieldChangeNode
segment: string | FromToIndex
}) {
if (typeof segment === 'string') {
return (
30 ? {maxWidth: 100} : {}}>
{segment}
)
}
const {hasMoved, fromIndex, toIndex, annotation} = segment
const created = typeof fromIndex === 'undefined'
const deleted = typeof toIndex === 'undefined'
if (created) {
// Item was created
return
}
if (deleted) {
// Item was deleted
return
}
if (hasMoved && typeof toIndex !== 'undefined' && typeof fromIndex !== 'undefined') {
// Item was moved
return
}
// Changed/unchanged
const readableIndex = (toIndex || 0) + 1
return (
#{readableIndex}
)
}
function CreatedTitleSegment({
annotation: annotationProp,
change,
toIndex = 0,
}: {
annotation: Annotation | undefined
change?: FieldChangeNode
toIndex?: number
}) {
const readableIndex = toIndex + 1
const description = `Added in position ${readableIndex}`
const content = <>#{readableIndex}>
const diffAnnotation = change?.diff ? getAnnotationAtPath(change.diff, []) : undefined
const annotation = diffAnnotation || annotationProp
if (annotation) {
return (
{content}
)
}
return (
{content}
)
}
function DeletedTitleSegment({
annotation,
fromIndex = 0,
}: {
annotation: Annotation | undefined
fromIndex?: number
}) {
const readableIndex = fromIndex + 1
const description = `Removed from position ${readableIndex}`
return (
#{readableIndex}
)
}
function MovedTitleSegment({
annotation,
fromIndex,
toIndex,
}: {
annotation: Annotation | undefined
fromIndex: number
toIndex: number
}) {
const indexDiff = toIndex - fromIndex
const indexSymbol = indexDiff < 0 ? '↑' : '↓'
const positions = Math.abs(indexDiff)
const description = `Moved ${positions} position${positions === 1 ? '' : 's'} ${
indexDiff < 0 ? 'up' : 'down'
}`
return (
<>
#{toIndex + 1}
{indexSymbol}
{Math.abs(indexDiff)}
>
)
}