var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { ImagePosition, TitlePosition } from '@dvn/alaca/components/dvn-card-header/models';
import { findField } from '@dvn/core/util/field';
import { getDataModelFieldDisplayValue } from './data-model';
export function generateCardItem(listConfig, fields, item) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
  const title = ((_a = listConfig.header) === null || _a === void 0 ? void 0 : _a.title) && getDataModelFieldDisplayValue(item, findField(fields, listConfig.header.title.fieldId), listConfig.header.title.fieldId);
  const titlePosition = ((_b = listConfig.header) === null || _b === void 0 ? void 0 : _b.title) ? listConfig.header.title.position : TitlePosition.BelowCover;
  const coverSrc = ((_d = (_c = listConfig.header) === null || _c === void 0 ? void 0 : _c.cover) === null || _d === void 0 ? void 0 : _d.fieldId) && item[findField(fields, listConfig.header.cover.fieldId).id];
  const coverSize = ((_e = listConfig.header) === null || _e === void 0 ? void 0 : _e.cover) && listConfig.header.cover.size;
  const coverAspectRatio = ((_f = listConfig.header) === null || _f === void 0 ? void 0 : _f.cover) && listConfig.header.cover.aspectRatio;
  const coverPlaceholder = ((_g = listConfig.header) === null || _g === void 0 ? void 0 : _g.cover) && listConfig.header.cover.placeholder;
  const imageSrc = ((_h = listConfig.header) === null || _h === void 0 ? void 0 : _h.image) && getDataModelFieldDisplayValue(item, findField(fields, listConfig.header.image.fieldId), listConfig.header.image.fieldId);
  const imagePlaceholderValue = ((_j = listConfig.header) === null || _j === void 0 ? void 0 : _j.image)
    && listConfig.header.image.placeholderFieldId
    && getDataModelFieldDisplayValue(item, findField(fields, listConfig.header.image.placeholderFieldId), listConfig.header.image.placeholderFieldId);
  const imagePlaceholderType = ((_k = listConfig.header) === null || _k === void 0 ? void 0 : _k.image) ? listConfig.header.image.placeholderType : 'text';
  const imageSize = ((_l = listConfig.header) === null || _l === void 0 ? void 0 : _l.image) && listConfig.header.image.size;
  const imageShape = ((_m = listConfig.header) === null || _m === void 0 ? void 0 : _m.image) ? listConfig.header.image.shape : 'circle';
  const imagePosition = ((_o = listConfig.header) === null || _o === void 0 ? void 0 : _o.image) ? listConfig.header.image.position : ImagePosition.Center;
  const bodyFields = (((_p = listConfig.body) === null || _p === void 0 ? void 0 : _p.fields) || []).map((bodyFieldConfig => {
    const { fieldId, fieldDisplay, customViewTag } = bodyFieldConfig, rest = __rest(bodyFieldConfig, ["fieldId", "fieldDisplay", "customViewTag"]);
    if (customViewTag) {
      return {
        customViewTag,
        field: undefined,
        label: '',
        value: '',
        meta: rest,
      };
    }
    const field = fieldDisplay || findField(fields, fieldId);
    if (!field) {
      console.error(`Field with id ${fieldId} not found. Available fields are ${fields.map(f => f.id).join(', ')}`);
    }
    return {
      field,
      label: field.title,
      value: getDataModelFieldDisplayValue(item, field),
      meta: rest,
    };
  }));
  return {
    header: {
      title,
      titlePosition,
      coverSrc,
      coverPlaceholder,
      coverSize,
      coverAspectRatio,
      imageSrc,
      imagePlaceholderValue,
      imagePlaceholderType,
      imageSize,
      imageShape,
      imagePosition,
      actions: ((_q = listConfig.header) === null || _q === void 0 ? void 0 : _q.actions) || [],
    },
    body: {
      fields: bodyFields,
    },
    footer: {
      actions: ((_r = listConfig.footer) === null || _r === void 0 ? void 0 : _r.actions) || [],
    }
  };
}
