import { createElement, lazy } from 'react'

const ListingInfo = lazy(
  () => import('../../components/Dashboard/Sales/Listings/ListingInfo')
)
const NewListingVarient = lazy(
  () => import('../../components/Dashboard/Sales/ListingVarients/NewListingVarient')
)
const PublishListing = lazy(
  () => import('../../components/Dashboard/Sales/Listings/PublishListing')
)
const UnpublishListing = lazy(
  () => import('../../components/Dashboard/Sales/Listings/UnpublishListing')
)
import ListingIcon from '../../components/Icons/ListingIcon'
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'
import PlusIcon from '../../components/Icons/PlusIcon'

export const Listing = {
  name: 'listing',
  label: 'Listing',
  labelPlural: 'Listings',
  url: '/dashboard/sales/listings',
  prefix: 'LST',
  icon: ListingIcon,
  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: 'newListingVarient',
      type: 'modal',
      modalWidth: 700,
      label: 'New Listing Varient',
      icon: PlusIcon,
      content: (objectData, { onOk } = {}) => {
        return createElement(NewListingVarient, { defaultValues: { listing: objectData }, onOk, reset: 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(PublishListing, { 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(UnpublishListing, { objectData, onOk })
      }
    },
    { type: 'divider' },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(ListingInfo)
    }
  ],
  columns: [
    '_reference',
    'title',
    'product',
    'vendor',
    'stockLocation',
    'marketplace',
    'courierServices',
    'state',
    'price',
    'currency',
    'lastSyncedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'product',
    'vendor',
    'stockLocation',
    'marketplace',
    'courierServices',
    'state',
    'state.type',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'title',
    'vendor',
    'state',
    'price',
    'lastSyncedAt',
    'createdAt',
    'updatedAt',
    '_id'
  ],
  group: [
    'marketplace',
    'product',
    'vendor',
    'stockLocation',
    'courierServices'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'listing',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'listing',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'title',
      label: 'Title',
      columnFixed: 'left',
      type: 'text',
      readOnly: false,
      required: true,
      columnWidth: 250,
      value: (objectData) => {
        if (objectData?.title == undefined) {
          return objectData?.product?.name
        }
        return objectData?.title
      }
    },
    {
      name: 'lastSyncedAt',
      label: 'Last Synced',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'product',
      label: 'Product',
      type: 'object',
      objectType: 'product',
      showHyperlink: true,
      readOnly: false,
      required: true,
      columnWidth: 200
    },
    {
      name: 'vendor',
      label: 'Vendor',
      type: 'object',
      objectType: 'vendor',
      showHyperlink: true,
      readOnly: false,
      required: true,
      value: (objectData) => {
        if (objectData?.vendor == undefined) {
          return objectData?.product?.vendor
        }
        return objectData?.vendor
      },
      columnWidth: 200
    },
    {
      name: 'stockLocation',
      label: 'Stock Location',
      type: 'object',
      objectType: 'stockLocation',
      showHyperlink: true,
      readOnly: false,
      required: true,
      columnWidth: 200
    },
    {
      name: 'marketplace',
      label: 'Marketplace',
      type: 'object',
      objectType: 'marketplace',
      showHyperlink: true,
      readOnly: false,
      required: true,
      columnWidth: 200
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'listing',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'price',
      label: 'Price',
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      readOnly: true,
      required: false,
      columnWidth: 120
    },
    {
      name: 'currency',
      label: 'Currency',
      type: 'text',
      readOnly: true,
      required: false,
      columnWidth: 100
    },
    {
      name: 'url',
      label: 'URL',
      type: 'url',
      readOnly: true,
      required: false,
      columnWidth: 250
    },
    {
      name: 'courierServices',
      label: 'Courier Services',
      type: 'objectList',
      multiple: true,
      objectType: 'courierService',
      readOnly: false,
      required: true,
      columnWidth: 250
    }
  ]
}
