import AsyncStorage from "@react-native-async-storage/async-storage"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; export type Fulfillment = "delivery" | "pickup"; export type PaymentMethod = | "chapa" | "telebirr" | "mpesa" | "pay_at_branch"; type LocationState = { fulfillment: Fulfillment; areaLabel: string; branchId: string | null; lat: number | null; lng: number | null; paymentPreference: PaymentMethod; setFulfillment: (f: Fulfillment) => void; setAreaLabel: (label: string) => void; setBranchId: (id: string | null) => void; setCoords: (lat: number | null, lng: number | null) => void; setPaymentPreference: (m: PaymentMethod) => void; allowedPayments: () => PaymentMethod[]; }; export const useLocationStore = create()( persist( (set, get) => ({ fulfillment: "delivery", areaLabel: "Addis Ababa", branchId: null, lat: null, lng: null, paymentPreference: "chapa", setFulfillment: (fulfillment) => { const next: Partial = { fulfillment }; if (fulfillment === "delivery" && get().paymentPreference === "pay_at_branch") { next.paymentPreference = "chapa"; } set(next); }, setAreaLabel: (areaLabel) => set({ areaLabel }), setBranchId: (branchId) => set({ branchId }), setCoords: (lat, lng) => set({ lat, lng }), setPaymentPreference: (paymentPreference) => set({ paymentPreference }), allowedPayments: () => { const base: PaymentMethod[] = ["chapa", "telebirr", "mpesa"]; return get().fulfillment === "pickup" ? [...base, "pay_at_branch"] : base; }, }), { name: "gishen-location", storage: createJSONStorage(() => AsyncStorage), } ) );