import {Action} from "../SelectActionPopupWindow";
import {__} from "../../globals";
import {closeSelectActionsPopupWindow} from "../hooks";
import {OfferData, TestableData, TestablePartial, useNodesStore} from "../store";
import {atomsStore, SelectActionPopupWindowStateAtom} from "../atoms";
import {QuantityMeta} from "../cards/Quantity";
import {equals, TierType} from "../tiers";
import {AmountValidatorOptions} from "../cards/filterinterfaces";
import {generateIdForType} from "../NodeHelpers";

export const SelectTier: Action = {
    id: 'select-tier',
    data: {
        name: 'Add Tiers',
    },
    screens: [
        {
            id: 2,
            title: __('Select the tier conditionals'),
            content: () => {
                return <div className="p-4 flex flex-col gap-4">
                    here we'll let the user choose from qunaoty, casrt subtotal, etc OR user role, ertc
                    <button className={'py-3 text-1x'}>Quantity</button>
                </div>
            },
        },
        {
            id: 3,
            title: () => {
                return __('Tier A: Select Quantity');
            },
            content: () => {
                return <div className="p-4 flex flex-col gap-4">
                    // HERE THE USER WILL CONFIGURE THE BASE TESTABES, EG: MIN, MAX, IN RANGE AND THE ACTUAL VALUE FOR THE FIRST TIER
                    <button className={'py-3 text-1x'}>1+</button>
                </div>
            },
            primaryBottom: () => {
                return {
                    label: __('Next: Select Offer'),
                    onClick() {
                        const addTreeNode = useNodesStore.getState().addTreeNode;
                        // @ts-ignore
                        const {context: {targetBranchId}} = atomsStore.get(SelectActionPopupWindowStateAtom)

                        const baseTestableId = generateIdForType('testable');
                        addTreeNode(
                            {
                                parentBranchTestableId: baseTestableId,
                                testable: {
                                    id: generateIdForType('testable.partial'),
                                    options: {
                                        quantity: equals(4)
                                    }
                                } as Omit<TestablePartial<AmountValidatorOptions>, 'id'>,
                                branch: {
                                    id: generateIdForType('branch'),
                                    type: 'offers',
                                    children: [
                                        {
                                            id: generateIdForType('offers'),
                                            type: 'Discount',
                                            options: {
                                                type: 'amount',
                                                amount: 20
                                            }
                                        } as OfferData
                                    ]
                                }
                            },
                            {
                                parentBranchId: targetBranchId,
                                newTieredBranchData: {
                                    baseTestable: {
                                        id: baseTestableId,
                                        type: QuantityMeta.id,
                                        testableType: 'filter',
                                        options: {
                                        }
                                    },
                                    tierType: TierType.Numeric,
                                }
                            }
                        )

                        // close this panel
                        closeSelectActionsPopupWindow()
                    }
                };
            }
        }
        // next we'll ask the user to add a offer, this is optional and if the user skips this, we'll add a select actions branchs to the tier
    ],
}