import { applicationState } from "../../state"
import * as reducers from "./reducers"
import { tap, ignoreElements, distinctUntilChanged } from "rxjs/operators"
import reducerFromMap from "@racletteCore/lib/reducerFromMap"
import { actions$, ofType } from "mini-rx-store"

const reducerMap = {
  init: () => applicationState.widgets,
  projectInit: reducers.projectInit,
  "widgets/update": reducers.widgetsUpdate,
  "widgets/add": reducers.widgetsAdd,
  "widgets/setConfig": reducers.widgetsSetConfig,
}

const widgetEffects = (store) => {
  const widgetAddEffects = actions$.pipe(
    ofType("widgets/add"),

    distinctUntilChanged((prev, curr) => !store.hasChanges(prev, curr)),

    ignoreElements(),
  )

  return [widgetAddEffects]
}
const widgetsReducers = reducerFromMap(reducerMap)

export { widgetEffects, widgetsReducers }
