"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _constants = require("../../constants");
var _helpers = require("../../state/helpers");
var _canShowAction = require("../../utils/actions/can-show-action");
var _extractInvokePreviewAction = require("../action/extract-invoke-preview-action");
var _lozenge = require("../common/lozenge");
var _extractServerAction = _interopRequireDefault(require("./extract-server-action"));
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) { (0, _defineProperty2.default)(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; }
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 = (0, _helpers.getExtensionKey)(response);
  var data = response === null || response === void 0 ? void 0 : response.data;
  var actions = (0, _extractServerAction.default)(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 = (0, _linkExtractors.extractLink)(data);
  var invokePreviewAction = response ? (_extractInvokePreview = (0, _extractInvokePreviewAction.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 = (0, _lozenge.extractLozenge)(response === null || response === void 0 ? void 0 : response.data);
  if (!lozenge) {
    return;
  }
  if (!(0, _canShowAction.canShowAction)(_constants.CardAction.ChangeStatusAction, actionOptions)) {
    return lozenge;
  }
  var action = extractAction(response, id, actionOptions, appearance, origin, fireEvent, resolve, isPreviewPanelAvailable, openPreviewPanel);
  return _objectSpread(_objectSpread({}, lozenge), {}, {
    action: action
  });
};
var _default = exports.default = extractState;