// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { fireEvent, waitFor } from '@testing-library/vue'
import { ref } from 'vue'

import { renderComponent } from '#tests/support/components/index.ts'

import CommonInlineEdit, {
  type Props,
} from '#desktop/components/CommonInlineEdit/CommonInlineEdit.vue'

const renderInlineEdit = (props: Partial<Props> = {}, options = {}) => {
  return renderComponent(CommonInlineEdit, {
    props: {
      name: 'inlineEditTest',
      value: 'test value',
      label: 'Inline Edit Label',
      submitLabel: 'Submit',
      cancelLabel: 'Cancel',
      ...props,
    },
    form: true,
    router: true,
    ...options,
  })
}

const stubCaretPosition = (getNode: () => Node | null, offset: number) => {
  Object.defineProperty(document, 'caretPositionFromPoint', {
    value: () => ({ offsetNode: getNode(), offset }),
    configurable: true,
  })
}

describe('CommonInlineEdit', () => {
  it('shows by default non editable node', () => {
    const wrapper = renderInlineEdit()

    expect(wrapper.getByText('test value')).toBeInTheDocument()
    expect(wrapper.queryByDisplayValue('test value')).not.toBeInTheDocument()
  })

  it('supports placeholder on edit input', async () => {
    const wrapper = renderInlineEdit({ placeholder: 'test placeholder' })
    await wrapper.events.click(wrapper.getByRole('button'))

    expect(await wrapper.findByPlaceholderText('test placeholder')).toBeInTheDocument()
  })

  it('submits edit on button click and enter', async () => {
    const submitEditCallbackSpy = vi.fn()

    const wrapper = renderInlineEdit({
      onSubmitEdit: (newValue: string) => submitEditCallbackSpy(newValue),
    })

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.type(wrapper.getByRole('textbox'), ' update')

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))

    expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update')

    expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  })

  it('submits edit on enter key', async () => {
    const submitEditCallbackSpy = vi.fn()

    const wrapper = renderInlineEdit({
      onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
    })

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await wrapper.events.keyboard('{enter}')

    expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  })

  it('submits on background click', async () => {
    const submitEditCallbackSpy = vi.fn()

    const wrapper = renderInlineEdit({
      onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
    })

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await fireEvent.click(document.body)

    expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  })

  it('do not stop edit mode when submit promise failed', async () => {
    const wrapper = renderInlineEdit({
      onSubmitEdit: (): Promise<void> => {
        return new Promise((resolve, reject) => {
          reject()
        })
      },
    })

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.type(wrapper.getByRole('textbox'), ' update')

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))

    expect(wrapper.getByRole('textbox')).toBeInTheDocument()
  })

  it('focuses field on edit', async () => {
    const wrapper = renderInlineEdit()

    await wrapper.events.click(wrapper.getByRole('button'))

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    expect(wrapper.getByRole('textbox')).toHaveFocus()
  })

  it('cancels on button click', async () => {
    const wrapper = renderInlineEdit()

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.click(wrapper.getByRole('button', { name: 'Cancel' }))

    expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()

    expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  })

  it('cancels on escape key', async () => {
    const wrapper = renderInlineEdit()

    await wrapper.events.click(wrapper.getByRole('button'))

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await wrapper.events.keyboard('{esc}')

    expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  })

  it.each([
    ['composing', { isComposing: true }],
    ['committing a composition in Safari', { keyCode: 229 }],
  ])('neither submits nor cancels on enter or escape while %s', async (_, eventInit) => {
    const submitEditCallbackSpy = vi.fn()

    const wrapper = renderInlineEdit({
      onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
    })

    await wrapper.events.click(wrapper.getByRole('button'))

    await wrapper.events.type(wrapper.getByRole('textbox'), ' update')

    const textbox = wrapper.getByRole('textbox')

    await fireEvent.keyDown(textbox, { key: 'Enter', ...eventInit })
    await fireEvent.keyDown(textbox, { key: 'Escape', ...eventInit })

    expect(submitEditCallbackSpy).not.toHaveBeenCalled()
    expect(wrapper.emitted()['cancel-edit']).toBeFalsy()
    expect(wrapper.getByRole('textbox')).toHaveValue('test value update')
  })

  it('disables submit if input is incorrect and required is true', async () => {
    const wrapper = renderInlineEdit({ required: true })

    await wrapper.events.click(wrapper.getByRole('button'))

    await waitFor(() => expect(wrapper.getByRole('textbox')).toBeInTheDocument())

    await wrapper.events.clear(wrapper.getByRole('textbox'))

    expect(wrapper.emitted()['submit-edit']).toBeFalsy()

    expect(wrapper.getByRole('button', { name: 'Submit' })).toBeDisabled()

    expect(wrapper.getByRole('textbox')).toBeInTheDocument()
  })

  it('supports adding attributes on label', () => {
    const wrapper = renderInlineEdit({
      labelAttrs: {
        role: 'heading',
        'aria-level': '1',
      },
    })

    expect(wrapper.getByRole('heading', { level: 1 })).toBeInTheDocument()
  })

  it('allows inline edit to be take up full width if set to block', () => {
    const wrapper = renderInlineEdit({
      block: true,
    })
    expect(wrapper.getByRole('button')).toHaveClass('w-full')
  })

  it('disables input if disabled prop is true', async () => {
    const wrapper = renderInlineEdit({ disabled: true })

    expect(wrapper.getByText('test value')).toBeInTheDocument()

    await wrapper.events.click(wrapper.getByText('test value'))

    expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  })

  it('detects links if set to true and renders it as link only in label', async () => {
    const wrapper = renderInlineEdit({
      detectLinks: true,
      value: 'https://zammad.com/en',
    })

    expect(wrapper.getByRole('link', { name: 'https://zammad.com/en' })).toBeInTheDocument()
  })

  it('keeps text and links in one element of the label, so they wrap as one paragraph', () => {
    const wrapper = renderInlineEdit({
      detectLinks: true,
      value: 'see https://zammad.com/en for details',
    })

    const label = wrapper.getByTestId('common-label')

    expect(label.children).toHaveLength(1)
    expect(label.firstElementChild).toHaveTextContent('see https://zammad.com/en for details')
  })

  it('displays initial edit value if editing got activated', async () => {
    const wrapper = renderInlineEdit({
      initialEditValue: 'initial Value',
      value: 'default Value',
    })

    await wrapper.events.click(wrapper.getByText('default Value'))

    expect(wrapper.getByRole('textbox')).toHaveValue('initial Value')
  })

  it('support loading', async () => {
    const wrapper = renderInlineEdit({
      initialEditValue: 'initial Value',
      value: 'default Value',
      loading: true,
    })

    await wrapper.events.click(wrapper.getByText('default Value'))

    expect(wrapper.getByRole('textbox')).toBeDisabled()
  })

  it('supports adding alternative background color', async () => {
    const wrapper = renderInlineEdit({
      alternativeBackground: true,
    })

    await wrapper.events.click(wrapper.getByText('test value'))

    expect(wrapper.html()).toContain('before:bg-neutral-50 dark:before:bg-gray-500')

    await wrapper.rerender({
      alternativeBackground: false,
    })

    expect(wrapper.html()).toContain('before:bg-blue-200 dark:before:bg-gray-700')

    expect(wrapper.html()).not.toContain('before:bg-neutral-50 dark:before:bg-gray-500')
  })

  it('supports limiting the maximum characters', async () => {
    const wrapper = renderInlineEdit({ maxLength: 10 })
    await wrapper.events.click(wrapper.getByText('test value'))
    const input = wrapper.getByRole('textbox')
    // native field element should have maxlength attribute set
    expect(input).toHaveAttribute('maxlength', '10')
  })

  it('edits in a textarea, unless limited to a single line', async () => {
    const wrapper = renderInlineEdit()

    await wrapper.events.click(wrapper.getByRole('button'))

    expect(wrapper.getByRole('textbox').tagName).toBe('TEXTAREA')

    await wrapper.rerender({ singleLine: true })

    expect(wrapper.getByRole('textbox').tagName).toBe('INPUT')
  })

  it('keeps line breaks, whether the value has them or they are pasted in', async () => {
    const submitEditCallbackSpy = vi.fn()

    const wrapper = renderInlineEdit({
      value: 'first\nsecond',
      onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
    })

    await wrapper.events.click(wrapper.getByRole('button'))
    await waitFor(() => expect(wrapper.getByRole('textbox')).toHaveFocus())

    await wrapper.events.type(wrapper.getByRole('textbox'), ' end')
    await wrapper.events.paste('\npasted')

    expect(wrapper.getByRole('textbox')).toHaveValue('first\nsecond end\npasted')

    await wrapper.events.keyboard('{enter}')

    expect(submitEditCallbackSpy).toHaveBeenCalledWith('first\nsecond end\npasted')
  })

  describe('caret position', () => {
    afterEach(() => {
      Reflect.deleteProperty(document, 'caretPositionFromPoint')
    })

    it('puts the caret where the text was clicked', async () => {
      const wrapper = renderInlineEdit()
      const label = wrapper.getByText('test value')

      stubCaretPosition(() => label.firstChild, 4)

      await wrapper.events.click(label)

      const textbox = wrapper.getByRole('textbox') as HTMLTextAreaElement
      await waitFor(() => expect(textbox).toHaveFocus())

      expect(textbox.selectionStart).toBe(4)
      expect(textbox.selectionEnd).toBe(4)
    })

    it('keeps the caret in place while typing', async () => {
      const wrapper = renderInlineEdit()
      const label = wrapper.getByText('test value')

      stubCaretPosition(() => label.firstChild, 4)

      await wrapper.events.click(label)

      const textbox = wrapper.getByRole('textbox') as HTMLTextAreaElement
      await waitFor(() => expect(textbox).toHaveFocus())

      await wrapper.events.keyboard('ing')

      expect(textbox).toHaveValue('testing value')
      expect(textbox.selectionStart).toBe(7)
    })

    it('puts the caret at the end when the edit value differs from the text', async () => {
      const wrapper = renderInlineEdit({
        initialEditValue: 'initial Value',
        value: 'default Value',
      })
      const label = wrapper.getByText('default Value')

      stubCaretPosition(() => label.firstChild, 4)

      await wrapper.events.click(label)

      const textbox = wrapper.getByRole('textbox') as HTMLTextAreaElement
      await waitFor(() => expect(textbox).toHaveFocus())

      expect(textbox.selectionStart).toBe('initial Value'.length)
    })

    it('puts the caret at the end when editing is started by keyboard', async () => {
      const wrapper = renderInlineEdit()

      stubCaretPosition(() => wrapper.getByText('test value').firstChild, 4)

      wrapper.getByRole('button').focus()
      await wrapper.events.keyboard('{enter}')

      const textbox = wrapper.getByRole('textbox') as HTMLTextAreaElement
      await waitFor(() => expect(textbox).toHaveFocus())

      expect(textbox.selectionStart).toBe('test value'.length)
    })

    it('puts the caret at the end when editing is started from outside after a click', async () => {
      const editing = ref(false)
      const wrapper = renderInlineEdit({}, { vModel: { editing } })
      const label = wrapper.getByText('test value')

      stubCaretPosition(() => label.firstChild, 4)

      await wrapper.events.click(label)
      await waitFor(() => expect(wrapper.getByRole('textbox')).toHaveFocus())
      await wrapper.events.click(wrapper.getByRole('button', { name: 'Cancel' }))

      editing.value = true

      const textbox = (await wrapper.findByRole('textbox')) as HTMLTextAreaElement
      await waitFor(() => expect(textbox).toHaveFocus())

      expect(textbox.selectionStart).toBe('test value'.length)
    })
  })
})
