'use client'

import lexicalImport from 'lexical'
const { $isRangeSelection, FORMAT_TEXT_COMMAND } = lexicalImport

import type { FeatureProviderProviderClient } from '../../types.js'

import { CodeIcon } from '../../../lexical/ui/icons/Code/index.js'
import { createClientComponent } from '../../createClientComponent.js'
import { SectionWithEntries } from '../common/floatingSelectToolbarSection.js'
import { INLINE_CODE } from './markdownTransformers.js'

const InlineCodeFeatureClient: FeatureProviderProviderClient<undefined> = (props) => {
  return {
    clientFeatureProps: props,
    feature: () => {
      return {
        clientFeatureProps: props,
        floatingSelectToolbar: {
          sections: [
            SectionWithEntries([
              {
                ChildComponent: CodeIcon,
                isActive: ({ selection }) => {
                  if ($isRangeSelection(selection)) {
                    return selection.hasFormat('code')
                  }
                  return false
                },
                key: 'code',
                onClick: ({ editor }) => {
                  editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'code')
                },
                order: 7,
              },
            ]),
          ],
        },

        markdownTransformers: [INLINE_CODE],
      }
    },
  }
}

export const InlineCodeFeatureClientComponent = createClientComponent(InlineCodeFeatureClient)
