/* doc-viewer.tsx generated by @compiled/babel-plugin v0.38.1 */
import "./doc-viewer.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useReducer } from 'react';
import { isCommonMediaClientError } from '@atlaskit/media-client';
import { DOCUMENT_SCROLL_ROOT_ID, DocumentViewer } from '@atlaskit/media-document-viewer';
import { useStaticCallback } from '@atlaskit/media-common';
import { MediaViewerError } from '../../errors';
import { ZoomControls } from '../../zoomControls';
import { ZoomLevel } from '../../domain/zoomLevel';
import { PasswordInput } from './passwordInput';
import Spinner from '@atlaskit/spinner';
const initialState = {
  isMissingPassword: false,
  hasPasswordError: false,
  password: '',
  zoomLevel: new ZoomLevel(1.75),
  isLoading: true
};
const reducer = (state, action) => {
  switch (action.type) {
    case 'submit_password':
      return {
        ...state,
        password: action.password
      };
    case 'set_zoom_level':
      return {
        ...state,
        zoomLevel: action.zoomLevel
      };
    case 'password_validation_failed':
      return {
        ...state,
        hasPasswordError: true
      };
    case 'password_validation_succeeded':
      return {
        ...state,
        isMissingPassword: false,
        hasPasswordError: false
      };
    case 'content_fetch_failed_password_required':
      return {
        ...state,
        isMissingPassword: true
      };
    case 'loading_completed':
      return {
        ...state,
        isLoading: false
      };
    default:
      return state;
  }
};
const documentViewerStyles = null;
const spinnerStyles = null;
export const DocViewer = ({
  mediaClient,
  fileState,
  collectionName,
  onError,
  onSuccess,
  traceContext
}) => {
  const [state, dispatch] = useReducer(reducer, initialState);
  const getContent = useStaticCallback(async (pageStart, pageEnd) => {
    try {
      return await mediaClient.mediaStore.getDocumentContent(fileState.id, {
        pageStart,
        pageEnd,
        collectionName,
        password: state.password
      }, traceContext);
    } catch (error) {
      if (isCommonMediaClientError(error) && error.reason === 'serverEntityLocked') {
        dispatch({
          type: 'content_fetch_failed_password_required'
        });
      } else {
        onError(new MediaViewerError('docviewer-content-fetch-failed', error instanceof Error ? error : undefined));
      }
      throw error;
    }
  });
  const onPasswordSubmit = useStaticCallback(async data => {
    dispatch({
      type: 'submit_password',
      password: data.password
    });
    try {
      // checking if the password is correct currently requires content fetch but we may have a faster enpoint for this in the future
      await mediaClient.mediaStore.getDocumentContent(fileState.id, {
        pageStart: 0,
        pageEnd: 1,
        collectionName,
        password: data.password
      }, traceContext);
      dispatch({
        type: 'password_validation_succeeded'
      });
    } catch (error) {
      if (isCommonMediaClientError(error) && error.reason === 'serverEntityLocked') {
        dispatch({
          type: 'password_validation_failed'
        });
      } else {
        onError(new MediaViewerError('docviewer-content-fetch-failed', error instanceof Error ? error : undefined));
      }
    }
  });
  const getPageImageUrl = useStaticCallback(async (pageNumber, zoom) => {
    const src = await mediaClient.mediaStore.getDocumentPageImage(fileState.id, {
      page: pageNumber,
      zoom,
      collectionName,
      password: state.password
    }, traceContext);
    return URL.createObjectURL(src);
  });
  const onSuccessHandler = useStaticCallback(() => {
    dispatch({
      type: 'loading_completed'
    });
    onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
  });
  const onZoomChange = useStaticCallback(newZoomLevel => {
    dispatch({
      type: 'set_zoom_level',
      zoomLevel: newZoomLevel
    });
  });
  if (state.isMissingPassword) {
    return /*#__PURE__*/React.createElement(PasswordInput, {
      onSubmit: onPasswordSubmit,
      hasPasswordError: state.hasPasswordError
    });
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, state.isLoading && /*#__PURE__*/React.createElement("div", {
    className: ax(["_kqswstnw _1e0c1txw _1bah1h6o _4cvr1h6o"])
  }, /*#__PURE__*/React.createElement(Spinner, {
    size: "large",
    appearance: "invert"
  })), /*#__PURE__*/React.createElement("div", {
    style: {
      opacity: state.isLoading ? 0 : 1
    },
    id: DOCUMENT_SCROLL_ROOT_ID,
    className: ax(["_1reo1wug _18m91wug _4t3i1kxc _1bsbauwl"])
  }, /*#__PURE__*/React.createElement(DocumentViewer, {
    getContent: getContent,
    getPageImageUrl: getPageImageUrl,
    zoom: state.zoomLevel.value,
    onSuccess: onSuccessHandler
  }), /*#__PURE__*/React.createElement(ZoomControls, {
    onChange: onZoomChange,
    zoomLevel: state.zoomLevel
  })));
};
export default DocViewer;