{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "custom-pricing-utils",
  "title": "Custom Pricing Utils",
  "description": "Custom pricing utils types and reducer store.",
  "files": [
    {
      "path": "registry/utils/custom-pricing-utils/index.tsx",
      "content": "import React from 'react';\n\nenum CUSTOM_PRICING_ACTION_TYPES {\n  REGISTER = 'register',\n  UPDATE_QTY = 'update_qty',\n  TOGGLE_CHECKED = 'toggle_checked',\n}\nexport interface CustomPricingProviderProps {\n  onChange?: (total: number) => void;\n  children?: React.ReactNode;\n}\ninterface CustomPricingQuantityItem {\n  type: 'quantity';\n  value: number;\n  unitPrice: number;\n}\nexport interface UseCustomPricingStoreOptions {\n  onChange?: (total: number) => void;\n}\n\ninterface CustomPricingCheckedItem {\n  type: 'checked';\n  checked: boolean;\n  price: number;\n  group?: string | null;\n}\ninterface CustomPricingContextValue\n  extends ReturnType<typeof useCustomPricingStore> {}\n\nexport type CustomPricingItem =\n  | CustomPricingQuantityItem\n  | CustomPricingCheckedItem;\n\nexport type CustomPricingState = Record<string, CustomPricingItem>;\n\nexport type CustomPricingAction =\n  | {\n      type: CUSTOM_PRICING_ACTION_TYPES.REGISTER;\n      id: string;\n      item: CustomPricingItem;\n    }\n  | { type: CUSTOM_PRICING_ACTION_TYPES.UPDATE_QTY; id: string; value: number }\n  | { type: CUSTOM_PRICING_ACTION_TYPES.TOGGLE_CHECKED; id: string };\n\nexport function customPricingReducer(\n  state: CustomPricingState,\n  action: CustomPricingAction,\n) {\n  switch (action.type) {\n    case CUSTOM_PRICING_ACTION_TYPES.REGISTER: {\n      if (state[action.id]) return state;\n      return { ...state, [action.id]: action.item };\n    }\n\n    case CUSTOM_PRICING_ACTION_TYPES.UPDATE_QTY: {\n      const item = state[action.id];\n      if (!item || item.type !== 'quantity') return state;\n      return { ...state, [action.id]: { ...item, value: action.value } };\n    }\n\n    case CUSTOM_PRICING_ACTION_TYPES.TOGGLE_CHECKED: {\n      const item = state[action.id];\n      if (!item || item.type !== 'checked') return state;\n\n      if (item.group) {\n        const updated = Object.fromEntries(\n          Object.entries(state).map(([k, v]) =>\n            v.type === 'checked' && v.group === item.group\n              ? [k, { ...v, checked: k === action.id }]\n              : [k, v],\n          ),\n        );\n        return updated;\n      }\n\n      return {\n        ...state,\n        [action.id]: { ...item, checked: !item.checked },\n      };\n    }\n\n    default:\n      return state;\n  }\n}\n\nexport function useCustomPricingStore({\n  onChange,\n}: UseCustomPricingStoreOptions = {}) {\n  const [items, dispatch] = React.useReducer(customPricingReducer, {});\n\n  const register = React.useCallback((id: string, item: CustomPricingItem) => {\n    dispatch({ type: CUSTOM_PRICING_ACTION_TYPES.REGISTER, id, item });\n  }, []);\n\n  const updateQuantity = React.useCallback((id: string, value: number) => {\n    dispatch({ type: CUSTOM_PRICING_ACTION_TYPES.UPDATE_QTY, id, value });\n  }, []);\n\n  const toggleChecked = React.useCallback((id: string) => {\n    dispatch({ type: CUSTOM_PRICING_ACTION_TYPES.TOGGLE_CHECKED, id });\n  }, []);\n\n  const total = React.useMemo(() => {\n    return Object.values(items).reduce((sum, item) => {\n      if (item.type === 'quantity') return sum + item.value * item.unitPrice;\n      if (item.type === 'checked') return sum + (item.checked ? item.price : 0);\n      return sum;\n    }, 0);\n  }, [items]);\n\n  const onChangeRef = React.useRef(onChange);\n  onChangeRef.current = onChange;\n\n  React.useEffect(() => {\n    onChangeRef.current?.(total);\n  }, [total]);\n\n  return { items, register, updateQuantity, toggleChecked, total };\n}\n\nexport const CustomPricingContext =\n  React.createContext<CustomPricingContextValue | null>(null);\n\nexport function useCustomPricing() {\n  const context = React.useContext(CustomPricingContext);\n  if (!context) {\n    throw new Error(\n      'useCustomPricing must be used within a CustomPricingProvider',\n    );\n  }\n  return context;\n}\ninterface UseCustomPricingQuantityProps {\n  id: string;\n  unitPrice?: number;\n  defaultValue?: number;\n}\nexport function useCustomPricingQuantity({\n  id,\n  unitPrice = 0,\n  defaultValue = 0,\n}: UseCustomPricingQuantityProps) {\n  const { register, updateQuantity, items } = useCustomPricing();\n  React.useEffect(() => {\n    register(id, { type: 'quantity', value: defaultValue, unitPrice });\n  }, [id, defaultValue, unitPrice, register]);\n\n  const item = items[id];\n  const quantity = item?.type === 'quantity' ? item.value : defaultValue;\n  const subtotal = quantity * unitPrice;\n\n  return {\n    quantity,\n    subtotal,\n    updateQuantity,\n  };\n}\ninterface UseCustomPricingSelectProps {\n  id: string;\n  price?: number;\n  type?: 'checkbox' | 'radio';\n  group?: string;\n  defaultChecked?: boolean;\n}\nexport function useCustomPricingSelect({\n  id,\n  price = 0,\n  type = 'radio',\n  group,\n  defaultChecked = false,\n}: UseCustomPricingSelectProps) {\n  const { register, toggleChecked, items } = useCustomPricing();\n  React.useEffect(() => {\n    register(id, {\n      type: 'checked',\n      checked: defaultChecked,\n      price,\n      group: type === 'radio' ? group : null,\n    });\n  }, [id, price, group, type, defaultChecked, register]);\n\n  const item = items[id];\n  const checked = item?.type === 'checked' ? item.checked : defaultChecked;\n\n  return {\n    checked,\n    toggleChecked,\n  };\n}\n",
      "type": "registry:item",
      "target": "components/systaliko-ui/utils/custom-pricing-utils.tsx"
    }
  ],
  "type": "registry:item"
}