import { ViewTypes } from 'nocodb-sdk'
import type { RowColoringInfo, ViewSettingOverrideOptions } from 'nocodb-sdk'
import { iconMap } from './iconUtils'
import type { Language } from '~/lib/types'
import UsersIcon from '~icons/nc-icons/users'
import LockIcon from '~icons/nc-icons-v2/lock'
import PersonalIcon from '~icons/nc-icons/personal'

export const viewIcons: Record<number | string, { icon: any; color: string; darkColor?: string }> = {
  [ViewTypes.GRID]: { icon: iconMap.grid, color: 'var(--color-view-icon-grid)' },
  [ViewTypes.FORM]: { icon: iconMap.form, color: 'var(--color-view-icon-form)' },
  [ViewTypes.CALENDAR]: { icon: iconMap.calendar, color: 'var(--color-view-icon-calendar)' },
  [ViewTypes.GALLERY]: { icon: iconMap.gallery, color: 'var(--color-view-icon-gallery)' },
  [ViewTypes.MAP]: { icon: iconMap.map, color: 'var(--color-view-icon-map)' },
  [ViewTypes.KANBAN]: { icon: iconMap.kanban, color: 'var(--color-view-icon-kanban)' },
  view: { icon: iconMap.view, color: 'var(--color-view-icon-view)' },
}

export const isRtlLang = (lang: keyof typeof Language) => ['fa', 'ar'].includes(lang)

const rtl = 'rtl' as const
const ltr = 'ltr' as const

export function applyLanguageDirection(dir: typeof rtl | typeof ltr) {
  const oppositeDirection = dir === ltr ? rtl : ltr

  document.body.classList.remove(oppositeDirection)
  document.body.classList.add(dir)
  document.body.style.direction = dir
}

export const getViewIcon = (key?: string | number) => {
  if (!key) return

  return viewIcons[key]
}

export function applyNonSelectable() {
  document.body.classList.add('non-selectable')
}

export const viewLockIcons = {
  [LockType.Personal]: {
    title: 'title.personal',
    icon: PersonalIcon,
    subtitle: 'msg.info.personalView',
  },
  [LockType.Collaborative]: {
    title: 'title.collaborative',
    icon: UsersIcon,
    subtitle: 'msg.info.collabView',
  },
  [LockType.Locked]: {
    title: 'title.locked',
    icon: LockIcon,
    subtitle: 'msg.info.lockedView',
  },
}

export const defaultRowColorInfo: RowColoringInfo = {
  mode: null,
  conditions: [],
  fk_column_id: null,
  color: null,
  is_set_as_background: null,
}

export const getDefaultViewMetas = (viewType: ViewTypes) => {
  switch (viewType) {
    case ViewTypes.FORM:
      return {
        submit_another_form: false,
        show_blank_form: false,
        meta: {
          hide_branding: false,
          background_color: '#F9F9FA',
          hide_banner: false,
        },
      }
  }
  return {}
}

export const validateViewConfigOverrideEvent = (
  event: SmartsheetStoreEvents | string,
  optionToValidate: ViewSettingOverrideOptions,
  params?: { viewId: string; copiedOptions: ViewSettingOverrideOptions[] },
) => {
  if (
    event !== SmartsheetStoreEvents.COPIED_VIEW_CONFIG ||
    !optionToValidate ||
    !ncIsObject(params) ||
    !ncIsArray(params?.copiedOptions)
  ) {
    return false
  }

  return params.copiedOptions.includes(optionToValidate)
}
