"use strict";
const toolkit = require("@reduxjs/toolkit");
const reducer = require("./reducer-D43h_jCk.js");
const api = require("./api-BygscvSz.js");
const common = require("./common-B47boGYI.js");
const state$3 = {
  users: [],
  departments: [],
  loading: true,
  error: "",
  isError: false
};
const shareSlice = toolkit.createSlice({
  name: "share",
  initialState: state$3,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(reducer.getAllUserAndDepartmentDropdown.pending, (state2) => {
      state2.loading = true;
      state2.error = "";
      state2.users = [];
      state2.departments = [];
      state2.isError = false;
    });
    builder.addCase(
      reducer.getAllUserAndDepartmentDropdown.fulfilled,
      (state2, action) => {
        var _a, _b, _c, _d, _e, _f;
        state2.loading = false;
        state2.users = (_c = (_b = (_a = action.payload) == null ? void 0 : _a.data) == null ? void 0 : _b.users) == null ? void 0 : _c.map((user) => ({
          ...user,
          label: `${user.first_name} ${user.last_name}`,
          value: user.id
        }));
        state2.departments = (_f = (_e = (_d = action.payload) == null ? void 0 : _d.data) == null ? void 0 : _e.roles) == null ? void 0 : _f.map((role) => ({
          ...role,
          label: role.name,
          value: role.id
        }));
        state2.error = "";
        state2.isError = false;
      }
    );
    builder.addCase(
      reducer.getAllUserAndDepartmentDropdown.rejected,
      (state2, action) => {
        state2.loading = false;
        state2.error = action.error.message || "";
        state2.isError = true;
      }
    );
  }
});
shareSlice.reducer;
const state$2 = {
  languagesData: {
    isLoading: false,
    isError: false,
    error: "",
    data: []
  },
  currentLanguage: {
    label: "English",
    code: "en",
    direction: "ltr"
  }
};
const fetchLanguages = toolkit.createAsyncThunk(
  "header/language/get",
  async () => {
    var _a, _b;
    const response = await api.getV1Languages({
      order: `id:1`,
      ...common.getToken()
    });
    const data = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.languages) || [];
    const languages = (_b = data == null ? void 0 : data.filter((d) => d == null ? void 0 : d.is_active)) == null ? void 0 : _b.map(
      (d) => {
        return {
          label: d == null ? void 0 : d.name,
          code: d == null ? void 0 : d.code,
          direction: d == null ? void 0 : d.direction
        };
      }
    );
    return { languages };
  }
);
const headerSlice = toolkit.createSlice({
  name: "header",
  initialState: state$2,
  reducers: {
    setLanguages(state2, { payload }) {
      state2.languagesData = { ...state2.languagesData, ...payload };
    },
    setCurrentLanguage(state2, { payload }) {
      state2.currentLanguage = { ...state2.currentLanguage, ...payload };
    }
  },
  extraReducers: (builder) => {
    builder.addCase(fetchLanguages.pending, (state2) => {
      state2.languagesData = {
        isLoading: true,
        isError: false,
        error: "",
        data: []
      };
    }).addCase(
      fetchLanguages.fulfilled,
      (state2, { payload }) => {
        state2.languagesData = {
          isLoading: false,
          isError: false,
          error: "",
          data: payload.languages || []
        };
      }
    ).addCase(
      fetchLanguages.rejected,
      (state2, { error }) => {
        const m = common.getErrorMessage(error.message);
        state2.languagesData = {
          isLoading: false,
          isError: true,
          error: m,
          data: []
        };
      }
    );
  }
});
const { setLanguages, setCurrentLanguage } = headerSlice.actions;
headerSlice.reducer;
const state$1 = {
  companies: [],
  isLoading: true,
  error: "",
  isError: false
};
const fetchCompanies = toolkit.createAsyncThunk(
  "reports-title/fetchCompanies",
  async () => {
    const response = await api.getV1Company({ ...common.getToken() });
    return response;
  }
);
const reportsTitleBarSlice = toolkit.createSlice({
  name: "reportsTitleBar",
  initialState: state$1,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchCompanies.pending, (state2) => {
      state2.isLoading = true;
      state2.error = "";
      state2.companies = [];
    });
    builder.addCase(fetchCompanies.fulfilled, (state2, action) => {
      var _a, _b;
      state2.companies = (_b = (_a = action.payload) == null ? void 0 : _a.data) == null ? void 0 : _b.company;
      state2.isLoading = false;
      state2.isError = false;
      state2.error = "";
    });
    builder.addCase(fetchCompanies.rejected, (state2, action) => {
      state2.isLoading = false;
      state2.error = action.error.message || "";
      state2.isError = true;
    });
  }
});
reportsTitleBarSlice.reducer;
const state = {
  companies: [],
  items: [],
  warehouseLocations: [],
  isLoading: true,
  assemblyItems: [],
  bomData: [],
  error: "",
  isError: false
};
const fetchOptions = toolkit.createAsyncThunk(
  "reports-title/fetchOptions",
  async () => {
    const i = await handleFetchItem();
    const c = await handleFetchCompanies();
    const wl = await handleFetchWarehouseLocations();
    const ai = await fetchAssemblyItems();
    return { i, c, wl, ai };
  }
);
const fetchBom = toolkit.createAsyncThunk(
  "reports-title/fetchBom",
  async () => {
    const bom = await fetchBomData();
    return bom;
  }
);
const handleFetchCompanies = async () => {
  var _a;
  try {
    const response = await api.getV1Company({ ...common.getToken() });
    return (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.company;
  } catch (error) {
    return [];
  }
};
const handleFetchItem = async () => {
  var _a;
  try {
    const response = await api.getV1InventoryItems({
      ...common.getToken(),
      skip: 0,
      limit: 999,
      filters: `(type.eq=inventory|type.eq=assembly&is_variant.eq=false|self_variant.eq=true)`
    });
    return (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.inventoryItemsList;
  } catch (error) {
    return [];
  }
};
const handleFetchWarehouseLocations = async () => {
  var _a;
  try {
    const response = await api.getV1WarehouseLocation({
      ...common.getToken(),
      skip: 0,
      limit: 999
    });
    return (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.warehouseLocationList;
  } catch (error) {
    return [];
  }
};
const fetchAssemblyItems = async () => {
  var _a;
  try {
    const response = await api.getV1AssemblyItems(
      {
        ...common.getToken(),
        skip: 0,
        limit: 999
      }
    );
    return (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.assemblyItemsList;
  } catch (error) {
    return [];
  }
};
const fetchBomData = async () => {
  var _a;
  try {
    const response = await api.getV1BillsOfMaterials({
      skip: 0,
      limit: 99999,
      ...common.getToken()
    });
    return (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.bom_list;
  } catch (error) {
    return [];
  }
};
const inventoryReportsTitleBarSlice = toolkit.createSlice({
  name: "inventoryReportsTitleBar",
  initialState: state,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchOptions.pending, (state2) => {
      state2.companies = [];
      state2.items = [];
      state2.warehouseLocations = [];
      state2.assemblyItems = [];
      state2.isLoading = true;
      state2.error = "";
    });
    builder.addCase(fetchOptions.fulfilled, (state2, action) => {
      var _a, _b, _c, _d;
      state2.companies = (_a = action.payload) == null ? void 0 : _a.c;
      state2.items = (_b = action.payload) == null ? void 0 : _b.i;
      state2.warehouseLocations = (_c = action.payload) == null ? void 0 : _c.wl;
      state2.assemblyItems = (_d = action.payload) == null ? void 0 : _d.ai;
      state2.isLoading = false;
      state2.isError = false;
      state2.error = "";
    });
    builder.addCase(fetchOptions.rejected, (state2, action) => {
      state2.isLoading = false;
      state2.error = action.error.message || "";
      state2.isError = true;
    });
    builder.addCase(fetchBom.fulfilled, (state2, action) => {
      var _a;
      state2.bomData = (_a = action.payload) == null ? void 0 : _a.bom;
    });
  }
});
inventoryReportsTitleBarSlice.reducer;
const libraryReducers = {
  share: shareSlice.reducer,
  header: headerSlice.reducer,
  reportsTitleBar: reportsTitleBarSlice.reducer,
  inventoryReportsTitleBar: inventoryReportsTitleBarSlice.reducer,
  dashboard: reducer.dashboardSlice.reducer,
  formBuilder: reducer.formBuilderListSlice.reducer
};
const createReducerManager = (initialReducers) => {
  let reducers = { ...initialReducers };
  let combinedReducer = toolkit.combineReducers(reducers);
  let keysToRemove = [];
  return {
    getReducers: () => ({ ...reducers }),
    reduce: (state2, action) => {
      if (keysToRemove.length) {
        state2 = { ...state2 };
        keysToRemove.forEach((key) => delete state2[key]);
        keysToRemove = [];
      }
      return combinedReducer(state2, action);
    },
    add: (key, reducer2) => {
      if (!key || reducers[key]) return;
      reducers = { ...reducers, [key]: reducer2 };
      combinedReducer = toolkit.combineReducers(reducers);
    },
    remove: (key) => {
      if (!key || !reducers[key]) return;
      const { [key]: _, ...rest } = reducers;
      reducers = rest;
      keysToRemove.push(key);
      combinedReducer = toolkit.combineReducers(reducers);
    }
  };
};
const createLibraryStore = (additionalReducers = {}, apiClient, middleware = []) => {
  const reducerManager = createReducerManager({
    ...libraryReducers,
    ...additionalReducers
  });
  const store2 = toolkit.configureStore({
    reducer: reducerManager.reduce,
    middleware: (getDefaultMiddleware) => getDefaultMiddleware({
      thunk: {
        extraArgument: { apiClient }
      }
    }).concat(middleware)
  });
  const reducerEnabledStore = {
    ...store2,
    reducerManager,
    injectReducer: (key, reducer2) => {
      reducerManager.add(key, reducer2);
      store2.replaceReducer(reducerManager.reduce);
    },
    removeReducer: (key) => {
      reducerManager.remove(key);
      store2.replaceReducer(reducerManager.reduce);
    }
  };
  return reducerEnabledStore;
};
const getLibrarySlices = () => {
  return {
    share: shareSlice.reducer,
    header: headerSlice.reducer,
    reportsTitleBar: reportsTitleBarSlice.reducer,
    inventoryReportsTitleBar: inventoryReportsTitleBarSlice.reducer,
    dashboard: reducer.dashboardSlice.reducer,
    formBuilder: reducer.formBuilderListSlice.reducer
  };
};
const store = createLibraryStore();
const injectLibraryReducer = (store2, key, reducer2) => {
  if (typeof store2.injectReducer === "function") {
    store2.injectReducer(key, reducer2);
  }
};
const removeLibraryReducer = (store2, key) => {
  if (typeof store2.removeReducer === "function") {
    store2.removeReducer(key);
  }
};
exports.createLibraryStore = createLibraryStore;
exports.fetchCompanies = fetchCompanies;
exports.getLibrarySlices = getLibrarySlices;
exports.injectLibraryReducer = injectLibraryReducer;
exports.libraryReducers = libraryReducers;
exports.removeLibraryReducer = removeLibraryReducer;
exports.store = store;
//# sourceMappingURL=store-C7nHRIec.js.map
