// import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
// import { RootState } from "./store";
// import {
//   Organization,
//   OrganizationOption,
// } from "@/dto/userRecords/organizationDto";
// import { LetterTemplatesDto } from "@/dto/userRecords/letterTemplatesDto";
// import { RecordsSettingsItem } from "@/dto/userRecords/recordsSettingDto";
// import { MultiSelectOptions, SelectOptions } from "@/common/commonTypes";
// import { recordTypeOptions } from "@/data/recordTypes";
// import { languageTypeOptions } from "@/data/languageTypes";
// import { TreeViewItem } from "@/common/treeSelect";
// import {
//   organizationOptions,
//   organizations as Organizations,
// } from "@/mock/organizations";
// import { recordsSettingsData } from "@/mock/recordsSettings";
// import { LetterTemplates } from "@/mock/letterTemplates";
// import { nonSmartOfficeOrgs } from "@/mock/nonSmartOfficeOrganizations";
// import { NonSmartDto } from "@/dto/userRecords/nonSmartOrgsDto";

// import { MultiSelectTypes } from "@/types/multiSelectTypes";

// import { UsersDto } from "@/dto/userRecords/usersDto";
// import { DepartmentDto } from "@/dto/userRecords/departmentsDto";

// import { fetchWithFallback } from "@/utils/utils";
// import ADService from "@/serivces/adService";
// import { users } from "@/mock/users";
// import { departments } from "@/mock/departments";
// import { SealDto } from "@/dto/sealDto";
// import { FileInformationDto } from "@/dto/approval/fileInformationDto";
// import { LibraryFilesDto } from "@/dto/userRecords/libraryFilesDto";

// export interface UserRecordsSettings {
//   signatureOptions: SelectOptions[];
//   teeterOptions: SelectOptions[];
//   prefixOptions: SelectOptions[];
//   ccPrefixOptions: SelectOptions[];
//   users: MultiSelectOptions[];
//   departments: MultiSelectOptions[];
//   hierarchyDepartments: MultiSelectOptions[];
//   organizationOptions: TreeViewItem[];
//   nonSmartOfficeReceivingOrganizations: MultiSelectOptions[];
//   suffixOptions: SelectOptions[];
//   ccSuffixOptions: SelectOptions[];
//   externalLetterTemplateOptions: SelectOptions[];
//   internalLetterTemplateOptions: SelectOptions[];
//   internalMemoLetterTemplateOptions: SelectOptions[];
//   count: number | null;
//   prefix: string | null;
//   prefixInternal: string | null;
//   suffixInternal: string | null;
//   recordTypes: SelectOptions[];
//   languages: SelectOptions[];
//   seals: SelectOptions[];
//   originalLetterTemplates: LetterTemplatesDto[];
//   settingsMap: Map<string, any>;
//   settings: RecordsSettingsItem[];
//   singleUsersOptions: SelectOptions[];
//   singleDepartmentsOptions: SelectOptions[];
// }

// interface UserRecordsSettingsState {
//   userRecordsSetting: UserRecordsSettings;
//   status: "idle" | "loading" | "failed" | "succeeded";
// }
// const initialState: UserRecordsSettingsState = {
//   userRecordsSetting: {
//     ccPrefixOptions: [],
//     ccSuffixOptions: [],
//     count: null,
//     externalLetterTemplateOptions: [],
//     internalLetterTemplateOptions: [],
//     internalMemoLetterTemplateOptions: [],
//     nonSmartOfficeReceivingOrganizations: [],
//     users: [],
//     departments: [],
//     hierarchyDepartments: [],
//     prefix: null,
//     prefixOptions: [],
//     organizationOptions: [],
//     suffixOptions: [],
//     recordTypes: [],
//     languages: [],
//     originalLetterTemplates: [],
//     seals: [],
//     prefixInternal: "",
//     suffixInternal: "",
//     settingsMap: new Map(),
//     settings: [],
//     signatureOptions: [],
//     teeterOptions: [],
//     singleDepartmentsOptions: [],
//     singleUsersOptions: [],
//   },
//   status: "idle",
// };

// const recordsSettingSharePointService = new SharePointService(
//   "Records_Setting"
// );
// const organizationsSharePointService = new SharePointService(
//   "Organizations",
//   `${window.location.origin}/sites/setting/`
// );

// const orgOptionsSharePointService = new SharePointService(
//   "Org Options",
//   `${window.location.origin}/sites/setting/`
// );
// const letterTemplatesSharePointService = new SharePointService(
//   "LetterTemplates"
// );
// const nonSmartOrgsSharePointService = new SharePointService(
//   "NotSmartOfficeOrganizations"
// );
// const sealsSharepointService = new SharePointService("Seals");

// const adService = new ADService();

// const teeterService = new SharePointService("Teeter");

// const signatureService = new SharePointService("Signature");
// // Type guards for runtime validation
// export function isRecordsSettings(
//   data: unknown
// ): data is RecordsSettingsItem[] {
//   return (
//     Array.isArray(data) && data.every((item) => "id" in item && "title" in item)
//   );
// }

// export function isOrganizations(data: unknown): data is Organization[] {
//   return (
//     Array.isArray(data) &&
//     data.every((item) => "orgId" in item && "orgName" in item)
//   );
// }

// export function isOrgOptions(data: unknown): data is OrganizationOption[] {
//   return (
//     Array.isArray(data) &&
//     data.every((item) => "optionId" in item && "optionValue" in item)
//   );
// }

// export function isLetterTemplates(data: unknown): data is LetterTemplatesDto[] {
//   return (
//     Array.isArray(data) &&
//     data.every((item) => "templateId" in item && "content" in item)
//   );
// }

// export function isNonSmartOrgs(data: unknown): data is NonSmartDto[] {
//   return (
//     Array.isArray(data) &&
//     data.every((item) => "orgCode" in item && "orgDescription" in item)
//   );
// }
// export const fetchUserRecordsOptions = createAsyncThunk<UserRecordsSettings>(
//   "/items/getUserRecordsSettings",
//   async () => {
//     const [
//       settings,
//       organizations,
//       orgOptions,
//       letterTemplates,
//       nonSmartOfficeReceivingOrganizations,
//       seals,
//       fetchedUsers,
//       fetchedDepartments,
//       fetchedSignatures,
//       fetchedTeeters,
//     ] = await Promise.all([
//       fetchWithFallback(
//         recordsSettingSharePointService.getItems(),
//         recordsSettingsData
//       ),
//       fetchWithFallback(
//         organizationsSharePointService.getItems(),
//         Organizations
//       ),
//       fetchWithFallback(
//         orgOptionsSharePointService.getItems(),
//         organizationOptions
//       ),
//       fetchWithFallback(
//         letterTemplatesSharePointService.getItems(),
//         LetterTemplates
//       ),
//       fetchWithFallback(
//         nonSmartOrgsSharePointService.getItems(),
//         nonSmartOfficeOrgs
//       ),
//       fetchWithFallback(
//         sealsSharepointService.getFilteredItems(``, ["FileRef", "FileLeafRef"]),
//         []
//       ),
//       fetchWithFallback(adService.getUsersUnderOrganization(), users),
//       fetchWithFallback(adService.getDepartmentsFromAD(), departments),
//       fetchWithFallback(
//         signatureService.getFilteredItems<FileInformationDto>(
//           `FileLeafRef eq '${adService.userDisplayName}.png'`,
//           ["FileLeafRef", "FileRef"],
//           [],
//           1,
//           "Id",
//           "desc"
//         ),
//         []
//       ),
//       fetchWithFallback(
//         teeterService.getFilteredItems<FileInformationDto>(
//           `FileLeafRef eq '${adService.userDisplayName} - English.png' or FileLeafRef eq '${adService.userDisplayName} - Amharic.png'`,
//           ["FileLeafRef", "FileRef"],
//           [],
//           2,
//           "Id",
//           "desc"
//         ),
//         []
//       ),
//     ]);

//     return processSettings(
//       settings as RecordsSettingsItem[],
//       organizations as Organization[],
//       orgOptions as OrganizationOption[],
//       letterTemplates as LetterTemplatesDto[],
//       seals as SealDto[],
//       fetchedUsers,
//       fetchedDepartments,
//       nonSmartOfficeReceivingOrganizations as NonSmartDto[],
//       fetchedSignatures as LibraryFilesDto[],
//       fetchedTeeters as LibraryFilesDto[]
//     );
//   }
// );

// const processSettings = (
//   settings: RecordsSettingsItem[],
//   organizations: Organization[],
//   orgOptions: OrganizationOption[],
//   letterTemplates: LetterTemplatesDto[],
//   seals: SealDto[],
//   fetchedUsers: UsersDto[],
//   fetchedDepartments: DepartmentDto[],
//   fetchedNonSmartOfficeReceivingOrganizations: NonSmartDto[],
//   fetchedSignatures: LibraryFilesDto[],
//   fetchedTeeters: LibraryFilesDto[]
// ): UserRecordsSettings => {
//   const parsedSettings = settings.map((setting) => ({
//     ...setting,
//     Value: sanitizeAndParse(setting.Value),
//     Id: setting.Id,
//   }));
//   const settingsMap = new Map(
//     parsedSettings.map((setting) => [setting.Title, setting.Value])
//   );

//   const mappedOrgs = mapRawDataToOrgListDTO(organizations);
//   const mappedOrgsOptions = mapRawDataToOrgOptionsListDTOGrouped(orgOptions);
//   const flattenedOrgOptions = Object.values(mappedOrgsOptions).flat();
//   const orgs = mapDataToTreeViewItems(mappedOrgs, flattenedOrgOptions);
//   const templates = mapDtoToLetterTemplates(letterTemplates);
//   const nonSmartOrg = mapDtoToString(
//     fetchedNonSmartOfficeReceivingOrganizations
//   );
//   const users: MultiSelectTypes[] =
//     mapUsersDtoToMultipleSelectOptions(fetchedUsers);
//   const departments: MultiSelectTypes[] =
//     mapDepartmentDtoToMultipleSelectOptions(fetchedDepartments);

//   const processedSettings: UserRecordsSettings = {
//     prefixOptions: mapStringsToSelectOptions(
//       settingsMap.get("Prefixes") as string[]
//     ),
//     prefixInternal:
//       settings.find((s: any) => s.Title === "PrefixInternalCC")?.Value || "",
//     suffixInternal:
//       settings.find((s: any) => s.Title === "SuffixInternalCC")?.Value || "",
//     ccPrefixOptions: mapStringsToSelectOptions(
//       settingsMap.get("Prefixes") as string[]
//     ),
//     suffixOptions: mapStringsToSelectOptions(
//       settingsMap.get("Suffixes") as string[]
//     ),
//     ccSuffixOptions: mapStringsToSelectOptions(
//       settingsMap.get("Suffixes") as string[]
//     ),
//     count: parseInt(settingsMap.get("Count") as string) || 0,
//     prefix: settingsMap.get("Prefix") as string,
//     organizationOptions: orgs,
//     nonSmartOfficeReceivingOrganizations:
//       mapStringsToMultiSelectOptions(nonSmartOrg),
//     externalLetterTemplateOptions: mapLetterTemplatesToSelectOptions(
//       templates.filter((letterTemplate) =>
//         letterTemplate.record_Type.includes("External")
//       )
//     ),
//     internalLetterTemplateOptions: mapLetterTemplatesToSelectOptions(
//       templates.filter((letterTemplate) =>
//         letterTemplate.record_Type.includes("Internal")
//       )
//     ),
//     internalMemoLetterTemplateOptions: mapLetterTemplatesToSelectOptions(
//       templates.filter((letterTemplate) =>
//         letterTemplate.record_Type.includes("InternalMemo")
//       )
//     ),
//     recordTypes: recordTypeOptions,
//     languages: languageTypeOptions,
//     originalLetterTemplates: letterTemplates,
//     users: users,
//     departments: departments,
//     hierarchyDepartments: departments,
//     seals: mapSealsToSelectOptions(seals),
//     settingsMap: settingsMap,
//     settings: settings,
//     signatureOptions: mapLibraryFilesToSelectOptions(fetchedSignatures),
//     teeterOptions: mapLibraryFilesToSelectOptions(fetchedTeeters),
//     singleUsersOptions: mapUsersDtoToSelectOptions(fetchedUsers),
//     singleDepartmentsOptions:
//       mapDepartmentDtoToSelectOptions(fetchedDepartments),
//   };

//   return processedSettings;
// };

// const settingsSlice = createSlice({
//   name: "settingsSlice",
//   initialState,
//   reducers: {},
//   extraReducers: (builders) => {
//     builders
//       .addCase(
//         fetchUserRecordsOptions.fulfilled,
//         (state, action: PayloadAction<UserRecordsSettings>) => {
//           state.userRecordsSetting = action.payload;
//           state.status = "idle";
//         }
//       )
//       .addCase(fetchUserRecordsOptions.pending, (state) => {
//         state.status = "loading";
//       })
//       .addCase(fetchUserRecordsOptions.rejected, (state) => {
//         state.userRecordsSetting = {
//           ccPrefixOptions: [],
//           ccSuffixOptions: [],
//           count: null,
//           externalLetterTemplateOptions: [],
//           internalLetterTemplateOptions: [],
//           internalMemoLetterTemplateOptions: [],
//           nonSmartOfficeReceivingOrganizations: [],
//           prefix: null,
//           prefixOptions: [],
//           organizationOptions: [],
//           suffixOptions: [],
//           recordTypes: recordTypeOptions,
//           users: [],
//           languages: languageTypeOptions,
//           originalLetterTemplates: [],
//           departments: [],
//           hierarchyDepartments: [],
//           prefixInternal: "",
//           suffixInternal: "",
//           seals: [],
//           settingsMap: new Map(),
//           settings: [],
//           signatureOptions: [],
//           teeterOptions: [],
//           singleDepartmentsOptions: [],
//           singleUsersOptions: [],
//         };
//         state.status = "succeeded";
//       });
//   },
// });

// export const userRecordsSettingsSelector = (state: RootState) =>
//   state.recordsOptionsReducer;

// export default settingsSlice.reducer;
