import { IPalmistryFinger, IPalmistryLine } from "@/api/resources/Palmistry";
import { createSlice, createSelector } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";

export type IPalmistryFingerLocal = IPalmistryFinger & { fingerName?: string }

interface IPalmistry {
  lines: IPalmistryLine[];
  fingers: IPalmistryFingerLocal[];
  photo: string;
  fromRedesign: boolean;
}

const initialState: IPalmistry = {
  lines: [],
  fingers: [],
  photo: "",
  fromRedesign: false
};

const palmistrySlice = createSlice({
  name: "palmistry",
  initialState,
  reducers: {
    update(state, action: PayloadAction<Partial<IPalmistry>>) {
      return { ...state, ...action.payload };
    }
  },
  extraReducers: (builder) => builder.addCase("reset", () => initialState),
});

export const { actions } = palmistrySlice;
export const selectPalmistryLines = createSelector(
  (state: { palmistry: IPalmistry }) => state.palmistry.lines,
  (palmistry) => palmistry
);
export const selectPalmistryFingers = createSelector(
  (state: { palmistry: IPalmistry }) => state.palmistry.fingers,
  (palmistry) => palmistry
);
export const selectPalmistryPhoto = createSelector(
  (state: { palmistry: IPalmistry }) => state.palmistry.photo,
  (palmistry) => palmistry
);
export const selectPalmistryFromRedesign = createSelector(
  (state: { palmistry: IPalmistry }) => state.palmistry.fromRedesign,
  (palmistry) => palmistry
);
export default palmistrySlice.reducer;
