import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { extractLink } from '@atlaskit/link-extractors';
import { CardAction } from '../../constants';
import { getExtensionKey } from '../../state/helpers';
import { canShowAction } from '../../utils/actions/can-show-action';
import { extractInvokePreviewAction } from '../action/extract-invoke-preview-action';
import { extractLozenge } from '../common/lozenge';
import extractServerAction from './extract-server-action';
var toInvokeRequest = function toInvokeRequest(extensionKey, resourceIdentifiers, actionType, details) {
  if (!actionType) {
    return;
  }
  return {
    action: {
      actionType: actionType,
      resourceIdentifiers: resourceIdentifiers
    },
    providerKey: extensionKey,
    details: details
  };
};
var extractAction = function extractAction(response, id, actionOptions, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel) {
  var _action$dataRetrieval, _extractInvokePreview, _action$dataUpdateAct;
  var extensionKey = getExtensionKey(response);
  var data = response === null || response === void 0 ? void 0 : response.data;
  var actions = extractServerAction(data);
  if (!extensionKey || actions.length === 0) {
    return;
  }
  var action = actions.find(function (item) {
    return (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;
  }
  var read = toInvokeRequest(extensionKey, action.resourceIdentifiers, (_action$dataRetrieval = action.dataRetrievalAction) === null || _action$dataRetrieval === void 0 ? void 0 : _action$dataRetrieval.name);
  var url = extractLink(data);
  var invokePreviewAction = response ? (_extractInvokePreview = extractInvokePreviewAction({
    actionOptions: actionOptions,
    appearance: appearance,
    fireEvent: fireEvent,
    id: id,
    onClose: resolve ? function () {
      return url && resolve(url, true);
    } : undefined,
    origin: origin,
    response: response,
    isPreviewPanelAvailable: isPreviewPanelAvailable,
    openPreviewPanel: openPreviewPanel
  })) === null || _extractInvokePreview === void 0 ? void 0 : _extractInvokePreview.invokeAction : undefined;
  var details = {
    id: id,
    url: url,
    invokePreviewAction: invokePreviewAction
  };
  var update = toInvokeRequest(extensionKey, action.resourceIdentifiers, (_action$dataUpdateAct = action.dataUpdateAction) === null || _action$dataUpdateAct === void 0 ? void 0 : _action$dataUpdateAct.name, details);
  return read || update ? {
    read: read,
    update: update
  } : undefined;
};
var extractState = function extractState(response, actionOptions, id, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel) {
  if (!response || !response.data) {
    return;
  }
  var lozenge = extractLozenge(response === null || response === void 0 ? void 0 : response.data);
  if (!lozenge) {
    return;
  }
  if (!canShowAction(CardAction.ChangeStatusAction, actionOptions)) {
    return lozenge;
  }
  var action = extractAction(response, id, actionOptions, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel);
  return _objectSpread(_objectSpread({}, lozenge), {}, {
    action: action
  });
};
export default extractState;