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

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import JobsDocumentsLocalGallery from "../../components/jobs-documents-local-gallery/jobs-documents-local-gallery.container";
const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(TemporaryDocsComponent);

export function TemporaryDocsComponent({ bodyshop }) {
  const { loading, error, data, refetch } = useQuery(QUERY_TEMPORARY_DOCS, {
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only",
    skip: bodyshop.uselocalmediaserver,
  });

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

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