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

export type ICompatibilityV2FingerLocal = IPalmistryFinger & {
  fingerName?: string;
};

interface ICompatibilityV2 {
  lines: IPalmistryLine[];
  fingers: ICompatibilityV2FingerLocal[];
  photo: string;
  fromRedesign: boolean;
}

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

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

export const { actions } = compatibilityV2Slice;
export const selectCompatibilityV2Lines = createSelector(
  (state: { compatibilityV2: ICompatibilityV2 }) => state.compatibilityV2.lines,
  (compatibilityV2) => compatibilityV2
);
export const selectCompatibilityV2Fingers = createSelector(
  (state: { compatibilityV2: ICompatibilityV2 }) =>
    state.compatibilityV2.fingers,
  (compatibilityV2) => compatibilityV2
);
export const selectCompatibilityV2Photo = createSelector(
  (state: { compatibilityV2: ICompatibilityV2 }) => state.compatibilityV2.photo,
  (compatibilityV2) => compatibilityV2
);
export const selectCompatibilityV2FromRedesign = createSelector(
  (state: { compatibilityV2: ICompatibilityV2 }) =>
    state.compatibilityV2.fromRedesign,
  (compatibilityV2) => compatibilityV2
);
export default compatibilityV2Slice.reducer;
