function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import { ACCESS_NONE } from '../../constants';
import { CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS } from './constants';
import { convertSharedLinkPermissions, convertSharedLinkSettings } from './utils';
export const createSharingService = ({
  hasSharedLink,
  itemApiInstance,
  onUpdateSharedLink,
  onRemoveSharedLink,
  options
}) => {
  const {
    id,
    permissions
  } = options;
  const changeSharedLinkAccess = async access => {
    if (!hasSharedLink) {
      return Promise.reject(_extends(new Error('Shared link not found'), {
        status: 404
      }));
    }
    return itemApiInstance.share({
      id,
      permissions
    }, access, onUpdateSharedLink, {}, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
  };
  const changeSharedLinkPermission = async permissionLevel => {
    if (!hasSharedLink) {
      return Promise.reject(_extends(new Error('Shared link not found'), {
        status: 404
      }));
    }
    return itemApiInstance.updateSharedLink({
      id,
      permissions
    }, {
      permissions: convertSharedLinkPermissions(permissionLevel)
    }, onUpdateSharedLink, {}, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
  };
  const updateSharedLink = async sharedLinkSettings => {
    if (!hasSharedLink) {
      return Promise.reject(_extends(new Error('Shared link not found'), {
        status: 404
      }));
    }
    const {
      access,
      isDownloadAvailable,
      serverUrl
    } = options;
    return new Promise((resolve, reject) => {
      itemApiInstance.updateSharedLink({
        id,
        permissions
      }, convertSharedLinkSettings(sharedLinkSettings, access, isDownloadAvailable, serverUrl), data => {
        onUpdateSharedLink(data);
        resolve(data);
      }, error => {
        reject(error);
      }, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
    });
  };
  const createSharedLink = async () => {
    return new Promise((resolve, reject) => {
      itemApiInstance.share({
        id,
        permissions
      }, undefined,
      // if "access" is undefined, the backend will set the default access level for the shared link
      data => {
        onUpdateSharedLink(data);
        resolve(data);
      }, error => {
        reject(error);
      }, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
    });
  };
  const deleteSharedLink = async () => {
    if (!hasSharedLink) {
      return Promise.reject(_extends(new Error('Shared link not found'), {
        status: 404
      }));
    }
    return new Promise((resolve, reject) => {
      itemApiInstance.share({
        id,
        permissions
      }, ACCESS_NONE, data => {
        onRemoveSharedLink(data);
        resolve(data);
      }, error => {
        reject(error);
      }, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
    });
  };
  return {
    createSharedLink,
    changeSharedLinkAccess,
    changeSharedLinkPermission,
    deleteSharedLink,
    updateSharedLink
  };
};
//# sourceMappingURL=sharingService.js.map