import { useQuery } from "@apollo/client";
import AlertComponent from "../../components/alert/alert.component";
import JobsDocumentsContainer from "../../components/jobs-documents-gallery/jobs-documents-gallery.container";
import LoadingSpinner from "../../components/loading-spinner/loading-spinner.component";
import { QUERY_TEMPORARY_DOCS } from "../../graphql/documents.queries";

import { useTreatmentsWithConfig } from "@splitsoftware/splitio-react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import JobsDocumentsLocalGallery from "../../components/jobs-documents-local-gallery/jobs-documents-local-gallery.container";
import { selectBodyshop } from "../../redux/user/user.selectors";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(TemporaryDocsComponent);

export function TemporaryDocsComponent({ bodyshop }) {
  const {
    treatments: { Imgproxy }
  } = useTreatmentsWithConfig({
    attributes: {},
    names: ["Imgproxy"],
    splitKey: bodyshop?.imexshopid
  });

  const { loading, error, data, refetch } = useQuery(QUERY_TEMPORARY_DOCS, {
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only",
    skip: Imgproxy.treatment === "on"
  });

  if (loading) return <LoadingSpinner />;
  if (error) return <AlertComponent message={error.message} type="error" />;

  if (bodyshop.uselocalmediaserver) {
    return <JobsDocumentsLocalGallery job={{ id: "temporary" }} />;
  }
  return (
    <JobsDocumentsContainer
      documentsList={data ? data.documents : []}
      jobId={null}
      billId={null}
      refetchOverride={refetch}
      ignoreSizeLimit
    />
  );
}
