'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-ce58ddc5.js');
const actionUtil = require('./action-util-5648f89e.js');
const models = require('./models-19fd9c92.js');
const field = require('./field-2133746c.js');
const dataModel = require('./data-model-9afb7f54.js');
const nested = require('./nested-8980065f.js');
require('./BaseField-d019ade6.js');

var __rest = (undefined && undefined.__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;
};
function generateCardItem(listConfig, fields, item) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
  const title = ((_a = listConfig.header) === null || _a === void 0 ? void 0 : _a.title) && dataModel.getDataModelFieldDisplayValue(item, field.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 : models.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[field.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) && dataModel.getDataModelFieldDisplayValue(item, field.findField(fields, (_j = listConfig.header.image) === null || _j === void 0 ? void 0 : _j.fieldId), (_k = listConfig.header.image) === null || _k === void 0 ? void 0 : _k.fieldId);
  const imagePlaceholderValue = ((_l = listConfig.header) === null || _l === void 0 ? void 0 : _l.image)
    && listConfig.header.image.placeholderFieldId
    && dataModel.getDataModelFieldDisplayValue(item, field.findField(fields, listConfig.header.image.placeholderFieldId), listConfig.header.image.placeholderFieldId);
  const imagePlaceholderType = ((_m = listConfig.header) === null || _m === void 0 ? void 0 : _m.image) ? listConfig.header.image.placeholderType : 'text';
  const imageSize = ((_o = listConfig.header) === null || _o === void 0 ? void 0 : _o.image) && listConfig.header.image.size;
  const imageShape = ((_p = listConfig.header) === null || _p === void 0 ? void 0 : _p.image) ? listConfig.header.image.shape : 'circle';
  const imagePosition = ((_q = listConfig.header) === null || _q === void 0 ? void 0 : _q.image) ? listConfig.header.image.position : models.ImagePosition.Center;
  const bodyFields = (((_r = listConfig.body) === null || _r === void 0 ? void 0 : _r.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$1 = fieldDisplay || field.findField(fields, fieldId);
    if (!field$1) {
      console.error(`Field with id ${fieldId} not found. Available fields are ${fields.map(f => f.id).join(', ')}`);
    }
    return {
      field: field$1,
      label: field$1.title,
      value: dataModel.getDataModelFieldDisplayValue(item, field$1),
      meta: rest,
    };
  }));
  return {
    header: {
      title,
      titlePosition,
      coverSrc,
      coverPlaceholder,
      coverSize,
      coverAspectRatio,
      imageSrc,
      imagePlaceholderValue,
      imagePlaceholderType,
      imageSize,
      imageShape,
      imagePosition,
      actions: ((_s = listConfig.header) === null || _s === void 0 ? void 0 : _s.actions) || [],
    },
    body: {
      fields: bodyFields,
    },
    footer: {
      actions: ((_t = listConfig.footer) === null || _t === void 0 ? void 0 : _t.actions) || [],
    }
  };
}

let DvnCardView = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.leaned = false;
  }
  render() {
    const { fields, item, config, overrideStyle } = this;
    const cardItem = generateCardItem(config, fields, item);
    // show cover if header config contains cover or image
    const showCover = !!config.header && !!(config.header.image || config.header.cover);
    // show image if header config contains image
    const showImage = !!config.header && !!config.header.image;
    // show header either title or image is present
    const showHeader = !!config.header && (config.header.title || showImage);
    return (index.h(index.Host, { style: this.overrideStyle }, index.h("dvn-card", { cornerRadius: this.cornerRadius, elevation: 0, leaned: this.leaned, style: overrideStyle }, showHeader &&
      index.h("dvn-card-header", { title: cardItem.header.title, titlePosition: cardItem.header.titlePosition, slot: "header", showCover: showCover, coverSrc: cardItem.header.coverSrc, coverPlaceholder: cardItem.header.coverPlaceholder, coverSize: cardItem.header.coverSize, coverAspectRatio: cardItem.header.coverAspectRatio, showImage: showImage, imageSrc: cardItem.header.imageSrc, imageSize: cardItem.header.imageSize, imageShape: cardItem.header.imageShape, imagePlaceholder: cardItem.header.imagePlaceholderValue, imagePlaceholderType: cardItem.header.imagePlaceholderType, imagePosition: cardItem.header.imagePosition, cornerRadius: this.cornerRadius }, cardItem.header.actions.length > 0 &&
        index.h("div", { class: "header-actions-container", slot: "action" }, cardItem.header.actions.map((action, index) => actionUtil.generateActionComponent(index, {}, item, this.action, action)))), cardItem.body.fields.map(field => {
      if (field.customViewTag) {
        const Tag = field.customViewTag;
        return (index.h(Tag, { item: item, field: field }));
      }
      else {
        return (index.h("dvn-field-display", { infoTitle: field.label, field: field.field, value: field.value, meta: field.meta }));
      }
    }), cardItem.footer.actions.length > 0 &&
      index.h("dvn-card-footer", { slot: "footer", alignRight: true }, cardItem.footer.actions.map((action, index) => actionUtil.generateActionComponent(index, {}, item, this.action, action))))));
    // return (
    //   <Host>
    //     <dvn-card leaned={true}>
    //       {config.body.map(fieldReference => {
    //         const field = findField(fields, fieldReference);
    //         if (!field) {
    //           return null;
    //         }
    //
    //         return (
    //           <dvn-field-display
    //             infoTitle={field.title}
    //             field={field}
    //             value={item[field.id]}
    //             meta={{
    //               lines: 3
    //             }}
    //           />
    //         );
    //       })}
    //     </dvn-card>
    //   </Host>
    // );
  }
};

const dvnFieldDisplayCss = ".sc-dvn-field-display-h{font-size:0.9em;margin:0px 0px calc(4 * var(--spacing-unit)) 0px;display:block}.sc-dvn-field-display-h:last-child{margin-bottom:0}.title.sc-dvn-field-display{font-size:0.8em;margin-bottom:calc(2 * var(--spacing-unit));color:var(--color-text-secondary)}";

const fieldTagMap = {
  number: 'dvn-field-display-number',
  'multiple-choice': 'dvn-field-display-multiple-choice',
  'single-choice': 'dvn-field-display-single-choice',
  text: 'dvn-field-display-text',
  date: 'dvn-field-display-date',
  attachment: 'dvn-field-display-attachment',
  'raw-data': 'dvn-field-display-raw-data',
  range: 'dvn-field-display-range',
  reference: 'dvn-field-display-reference',
  'relation-reference': 'dvn-field-display-relation-reference',
};
let DvnFieldDisplay = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.showTitle = true;
  }
  render() {
    const { field, infoTitle, meta, showTitle, value } = this;
    let fieldToPass = field;
    let Tag = fieldTagMap[field.type];
    if (field.type === nested.NestedField.type) {
      Tag = fieldTagMap[field.nestedField.type];
      fieldToPass = field.nestedField;
    }
    return (index.h(index.Host, null, showTitle && index.h("div", { class: "title" }, (infoTitle || '').toUpperCase()), Tag
      ? index.h(Tag, { field: fieldToPass, value: value, meta: meta, fixHeight: false })
      : index.h("span", null, value)));
  }
};
DvnFieldDisplay.style = dvnFieldDisplayCss;

exports.dvn_card_view = DvnCardView;
exports.dvn_field_display = DvnFieldDisplay;
