import { extractLink } from '@atlaskit/link-extractors';
import { getExtensionKey } from '../../state/helpers';
import { canShowAction } from '../../utils/actions/can-show-action';
import { CardAction } from '../../view/Card/types';
import { extractInvokePreviewAction } from '../action/extract-invoke-preview-action';
import { extractLozenge } from '../common/lozenge';
import extractServerAction from './extract-server-action';
const toInvokeRequest = (extensionKey, resourceIdentifiers, actionType, details) => {
  if (!actionType) {
    return;
  }
  return {
    action: {
      actionType,
      resourceIdentifiers
    },
    providerKey: extensionKey,
    details
  };
};
const extractAction = (response, id, actionOptions, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel) => {
  var _action$dataRetrieval, _extractInvokePreview, _action$dataUpdateAct;
  const extensionKey = getExtensionKey(response);
  const data = response === null || response === void 0 ? void 0 : response.data;
  const actions = extractServerAction(data);
  if (!extensionKey || actions.length === 0) {
    return;
  }
  const action = actions.find(item => (item === null || item === void 0 ? void 0 : item.name) === 'UpdateAction' && (item === null || item === void 0 ? void 0 : item.refField) === 'tag');
  if (!action || !action.resourceIdentifiers) {
    return;
  }
  const read = toInvokeRequest(extensionKey, action.resourceIdentifiers, (_action$dataRetrieval = action.dataRetrievalAction) === null || _action$dataRetrieval === void 0 ? void 0 : _action$dataRetrieval.name);
  const url = extractLink(data);
  const invokePreviewAction = response ? (_extractInvokePreview = extractInvokePreviewAction({
    actionOptions,
    appearance,
    fireEvent,
    id,
    onClose: resolve ? () => url && resolve(url, true) : undefined,
    origin,
    response,
    isPreviewPanelAvailable,
    openPreviewPanel
  })) === null || _extractInvokePreview === void 0 ? void 0 : _extractInvokePreview.invokeAction : undefined;
  const details = {
    id,
    url,
    invokePreviewAction
  };
  const update = toInvokeRequest(extensionKey, action.resourceIdentifiers, (_action$dataUpdateAct = action.dataUpdateAction) === null || _action$dataUpdateAct === void 0 ? void 0 : _action$dataUpdateAct.name, details);
  return read || update ? {
    read,
    update
  } : undefined;
};
const extractState = (response, actionOptions, id, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel) => {
  if (!response || !response.data) {
    return;
  }
  const lozenge = extractLozenge(response === null || response === void 0 ? void 0 : response.data);
  if (!lozenge) {
    return;
  }
  if (!canShowAction(CardAction.ChangeStatusAction, actionOptions)) {
    return lozenge;
  }
  const action = extractAction(response, id, actionOptions, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel);
  return {
    ...lozenge,
    action
  };
};
export default extractState;