import Condition from '../Condition';
import FieldsGroup from '../../FieldsGroup';
import { invert } from 'lodash';
import Currency from '../../Currency';

export default class CustomerOrderHistory extends Condition
{
    static TYPE = window?.CouponsPlus?.components?.conditions?.CustomerOrderHistory?.type;
    
    getComponent() 
    {
        return window.CouponsPlus.components.conditions.CustomerOrderHistory;       
    }

    getTitle() 
    {
        return window.CouponsPlus.components.conditions.CustomerOrderHistory.name;
    }

    getDescription() 
    {
        return window.CouponsPlus.components.conditions.CustomerOrderHistory.description;
    }
    
    getIcon()
{
        return (<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
  <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 10.5V6a3.75 3.75 0 1 0-7.5 0v4.5m11.356-1.993 1.263 12c.07.665-.45 1.243-1.119 1.243H4.25a1.125 1.125 0 0 1-1.12-1.243l1.264-12A1.125 1.125 0 0 1 5.513 7.5h12.974c.576 0 1.059.435 1.119 1.007ZM8.625 10.5a.375.375 0 1 1-.75 0 .375.375 0 0 1 .75 0Zm7.5 0a.375.375 0 1 1-.75 0 .375.375 0 0 1 .75 0Z" />
</svg>
);
    }

    getFields() 
    {
        return [
            new FieldsGroup([
                {
                    type: 'select',
                    width: 'auto',
                    options:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.numberOfOrders.quantity.type.meta['_allowed'],
                    labels: {
                        left: __('Has',window.CouponsPlus.textDomain)
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.numberOfOrders.quantity.type',
                    getValue: () => {
                        return invert(CouponsPlus.components.conditions.CustomerOrderHistory.fields.numberOfOrders.quantity.type.meta['_allowed'])[this.data.options.numberOfOrders.quantity.type]
                    },
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        right: __('orders',window.CouponsPlus.textDomain)
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.numberOfOrders.quantity.amount',
                    getValue: () => {
                        return this.data.options.numberOfOrders.quantity.amount
                    },
                    show: () => this.data.options.numberOfOrders.quantity.type !== 'range'
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        left:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.numberOfOrders.quantity.range.minimum.meta.name,
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.numberOfOrders.quantity.range.minimum',
                    getValue: () => {
                        return this.data.options.numberOfOrders.quantity.range.minimum
                    },
                    show: () => this.data.options.numberOfOrders.quantity.type === 'range'
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        left:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.numberOfOrders.quantity.range.maxmimum.meta.name,
                        right: __('items',window.CouponsPlus.textDomain)
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.numberOfOrders.quantity.range.maxmimum',
                    getValue: () => {
                        return this.data.options.numberOfOrders.quantity.range.maxmimum
                    },
                    show: () => this.data.options.numberOfOrders.quantity.type === 'range'
                },
            ]),
            new FieldsGroup([
                {
                    type: 'select',
                    width: 'auto',
                    options:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.enabled.meta['_allowed'],
                    labels: {
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.filters.price.enabled',
                    getValue: () => {
                        return invert(CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.enabled.meta['_allowed'])[this.data.options.filters.price.enabled]
                    },
                },
                {
                    type: 'select',
                    width: 'auto',
                    options:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.options.quantity.type.meta['_allowed'],
                    labels: {
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.filters.price.options.quantity.type',
                    getValue: () => {
                        return invert(CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.options.quantity.type.meta['_allowed'])[this.data.options.filters.price.options.quantity.type]
                    },
                    show: () => this.data.options.filters.price.enabled
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        inside: {
                            left: <Currency />
                        }
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.filters.price.options.quantity.amount',
                    getValue: () => {
                        return this.data.options.filters.price.options.quantity.amount
                    },
                    show: () => this.data.options.filters.price.enabled && this.data.options.filters.price.options.quantity.type !== 'range'
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        left:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.options.quantity.range.minimum.meta.name,
                        inside: {
                            left: <Currency />
                        }
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.filters.price.options.quantity.range.minimum',
                    getValue: () => {
                        return this.data.options.filters.price.options.quantity.range.minimum
                    },
                    show: () => this.data.options.filters.price.enabled && this.data.options.filters.price.options.quantity.type === 'range'
                },
                {
                    type: 'input',
                    subtype: 'number',
                    width: '',
                    labels: {
                        left:window.CouponsPlus.components.conditions.CustomerOrderHistory.fields.filters.price.options.quantity.range.maxmimum.meta.name,
                        inside: {
                            left: <Currency />
                        }
                    },
                    temporaryID: this.data.temporaryID,
                    propertyName: 'options.filters.price.options.quantity.range.maxmimum',
                    getValue: () => {
                        return this.data.options.filters.price.options.quantity.range.maxmimum
                    },
                    show: () => this.data.options.filters.price.enabled && this.data.options.filters.price.options.quantity.type === 'range'
                },
            ])
        ]
    }

    getDefaultOptions()
    {
        return window?.CouponsPlus?.components?.conditions?.CustomerOrderHistory?.defaultOptions;
    }
}