import { createElement, lazy } from 'react'

const AppPasswordInfo = lazy(
  () =>
    import('../../components/Dashboard/Management/AppPasswords/AppPasswordInfo')
)
const RegenerateAppPasswordSecret = lazy(
  () =>
    import('../../components/Dashboard/Management/AppPasswords/RegenerateAppPasswordSecret')
)
import AppPasswordIcon from '../../components/Icons/AppPasswordIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import LockIcon from '../../components/Icons/LockIcon'

export const AppPassword = {
  name: 'appPassword',
  label: 'App Password',
  labelPlural: 'App Passwords',
  url: '/dashboard/management/apppasswords',
  prefix: 'APP',
  icon: AppPasswordIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'edit',
      type: 'page',
      pageName: 'info',
      label: 'Edit',
      row: true,
      icon: EditIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      disabled: (objectData) => {
        return objectData?._user?._id != objectData?.user?._id
      }
    },
    {
      name: 'cancelEdit',
      label: 'Cancel Edits',
      type: 'page',
      pageName: 'info',
      icon: XMarkIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'finishEdit',
      label: 'Save Edits',
      type: 'page',
      pageName: 'info',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    { type: 'divider' },
    {
      name: 'regenerateSecret',
      type: 'modal',
      modalWidth: 630,
      label: 'Regenerate Secret',
      row: true,
      icon: LockIcon,
      disabled: (objectData) => {
        return objectData?._user?._id != objectData?.user?._id
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(RegenerateAppPasswordSecret, {
          id: objectData._id,
          onOk
        })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(AppPasswordInfo)
    }
  ],
  columns: ['_reference', 'name', 'user', 'active', 'createdAt', 'updatedAt'],
  filters: [
    '_id',
    'name',
    'user',
    'active',
    'user._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['name', 'user', 'active', 'createdAt', 'updatedAt'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'appPassword',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'appPassword',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'name',
      label: 'Name',
      columnFixed: 'left',
      required: true,
      type: 'text',
      columnWidth: 200
    },

    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'user',
      label: 'User',
      required: true,
      readOnly: (objectData) => {
        return objectData?.createdAt != null
      },
      type: 'object',
      objectType: 'user',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'active',
      label: 'Active',
      required: true,
      type: 'bool',
      columnWidth: 125
    },
    {
      name: 'secret',
      label: 'Secret',
      type: 'password',
      required: false,
      readOnly: true,
      value: (objectData) =>
        objectData?._id ? '••••••••••••••••••••••••••••••••' : undefined,
      columnWidth: 200
    }
  ]
}
