import { RootState } from "app/store";
import { UPLOAD_FILE_SIZE_LIMIT } from "./constants";

export const selectTemplates = (state: RootState) =>
  state.template.domain.templates;

export const selectIsLoading = (state: RootState) =>
  state.template.apps.isLoading;

export const selectIsUploading = (state: RootState) =>
  state.template.apps.isUploading;

export const selectUploadFile = (state: RootState) =>
  state.template.apps.uploadFile;

export const selectUploadFileError = (state: RootState) => {
  const { uploadFile } = state.template.apps;

  // 必須チェック
  if (!uploadFile) {
    return { hasErrorRequired: true, hasErrorFileSize: false };
  }
  // ファイルサイズチェック(5MB)
  if (uploadFile.size > UPLOAD_FILE_SIZE_LIMIT) {
    return { hasErrorRequired: false, hasErrorFileSize: true };
  }

  return { hasErrorRequired: false, hasErrorFileSize: false };
};
