import { createElement, lazy } from 'react'

const ListingVarientInfo = lazy(
  () => import('../../components/Dashboard/Sales/ListingVarients/ListingVarientInfo')
)
const PublishListingVarient = lazy(
  () => import('../../components/Dashboard/Sales/ListingVarients/PublishListingVarient')
)
const UnpublishListingVarient = lazy(
  () => import('../../components/Dashboard/Sales/ListingVarients/UnpublishListingVarient')
)
import ListingVarientIcon from '../../components/Icons/ListingVarientIcon'
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 BinIcon from '../../components/Icons/BinIcon'

export const ListingVarient = {
  name: 'listingVarient',
  url: '/dashboard/sales/listingvarients',
  label: 'Listing Varient',
  labelPlural: 'Listing Varients',
  prefix: 'LVR',
  icon: ListingVarientIcon,
  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)
      }
    },
    {
      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: 'publish',
      type: 'modal',
      modalWidth: 520,
      label: 'Publish',
      icon: CheckIcon,
      visible: (objectData) =>
        objectData?.state?.type === 'draft' ||
        objectData?.state?.type === 'inactive',
      disabled: (objectData) => objectData?.state?.type === 'syncing',
      content: (objectData, { onOk } = {}) => {
        return createElement(PublishListingVarient, { objectData, onOk })
      }
    },
    {
      name: 'unpublish',
      type: 'modal',
      modalWidth: 520,
      label: 'Unpublish',
      icon: XMarkIcon,
      danger: true,
      visible: (objectData) => objectData?.state?.type === 'active',
      disabled: (objectData) => objectData?.state?.type === 'syncing',
      content: (objectData, { onOk } = {}) => {
        return createElement(UnpublishListingVarient, { objectData, onOk })
      }
    },
    { type: 'divider' },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(ListingVarientInfo)
    }
  ],
  columns: [
    '_reference',
    'listing',
    'product',
    'productSku',
    'state',
    'price',
    'currency',
    'lastSyncedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'listing',
    'listing._id',
    'product',
    'productSku',
    'state',
    'state.type',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['state', 'price', 'lastSyncedAt', 'createdAt', 'updatedAt', '_id'],
  group: ['listing', 'state'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'listingVarient',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'listingVarient',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'listing',
      label: 'Listing',
      type: 'object',
      objectType: 'listing',
      showHyperlink: true,
      readOnly: false,
      required: true,
      columnWidth: 200
    },
    {
      name: 'product',
      label: 'Product',
      type: 'object',
      objectType: 'product',
      showHyperlink: true,
      readOnly: true,
      value: (objectData) => {
        return objectData?.listing?.product
      },
      required: true,
      columnWidth: 200
    },
    {
      name: 'productSku',
      label: 'Product SKU',
      type: 'object',
      objectType: 'productSku',
      showHyperlink: true,
      readOnly: false,
      required: true,
      columnWidth: 200,
      masterFilter: (objectData) => {
        return { product: objectData?.listing?.product?._id }
      }
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'listingVarient',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'price',
      label: 'Price',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: false,
      required: false,
      columnWidth: 120
    },
    {
      name: 'currency',
      label: 'Currency',
      type: 'text',
      readOnly: false,
      required: false,
      columnWidth: 100
    },
    {
      name: 'priceTaxRate',
      label: 'Tax Rate',
      type: 'object',
      objectType: 'taxRate',
      showHyperlink: true,
      required: false,
      columnWidth: 150
    },
    {
      name: 'priceWithTax',
      label: 'Price w/ Tax',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: true,
      required: false,
      columnWidth: 130
    },
    {
      name: 'lastSyncedAt',
      label: 'Last Synced',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    }
  ]
}
