'use strict';

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

const index = require('./index-7cdf1df1.js');
require('./index-c2a077ae.js');
const dataModel = require('./data-model-f9df493f.js');
require('./BaseField-f50bb8f0.js');
const nested = require('./nested-14baa4ef.js');
const field = require('./field-161bf2df.js');
const actionUtil = require('./action-util-fa9807bd.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;
  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 coverSrc = ((_c = (_b = listConfig.header) === null || _b === void 0 ? void 0 : _b.cover) === null || _c === void 0 ? void 0 : _c.fieldId) && item[field.findField(fields, listConfig.header.cover.fieldId).id];
  const coverSize = ((_d = listConfig.header) === null || _d === void 0 ? void 0 : _d.cover) && listConfig.header.cover.size;
  const coverAspectRatio = ((_e = listConfig.header) === null || _e === void 0 ? void 0 : _e.cover) && listConfig.header.cover.aspectRatio;
  const coverPlaceholder = ((_f = listConfig.header) === null || _f === void 0 ? void 0 : _f.cover) && listConfig.header.cover.placeholder;
  const imageSrc = ((_g = listConfig.header) === null || _g === void 0 ? void 0 : _g.image) && dataModel.getDataModelFieldDisplayValue(item, field.findField(fields, listConfig.header.image.fieldId), listConfig.header.image.fieldId);
  const imagePlaceholderValue = ((_h = listConfig.header) === null || _h === void 0 ? void 0 : _h.image) && listConfig.header.image.placeholderFieldId
    && item[field.findField(fields, listConfig.header.image.placeholderFieldId).id];
  const imagePlaceholderType = ((_j = listConfig.header) === null || _j === void 0 ? void 0 : _j.image) ? listConfig.header.image.placeholderType : 'text';
  const imageSize = ((_k = listConfig.header) === null || _k === void 0 ? void 0 : _k.image) && listConfig.header.image.size;
  const imageShape = ((_l = listConfig.header) === null || _l === void 0 ? void 0 : _l.image) ? listConfig.header.image.shape : 'circle';
  const imagePosition = ((_m = listConfig.header) === null || _m === void 0 ? void 0 : _m.image) ? listConfig.header.image.position : 'center'; // TODO ImagePosition.Center;
  const bodyFields = (listConfig.body.fields || []).map((bodyFieldConfig => {
    const { fieldId } = bodyFieldConfig, rest = __rest(bodyFieldConfig, ["fieldId"]);
    const field$1 = bodyFieldConfig.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,
      coverSrc,
      coverPlaceholder,
      coverSize,
      coverAspectRatio,
      imageSrc,
      imagePlaceholderValue,
      imagePlaceholderType,
      imageSize,
      imageShape,
      imagePosition,
      actions: ((_o = listConfig.header) === null || _o === void 0 ? void 0 : _o.actions) || [],
    },
    body: {
      fields: bodyFields,
    },
    footer: {
      actions: ((_p = listConfig.footer) === null || _p === void 0 ? void 0 : _p.actions) || [],
    }
  };
}

const 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, 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, cornerRadius: this.cornerRadius, imagePosition: cardItem.header.imagePosition }, 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 => (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',
  multipleChoice: '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',
  rawData: 'dvn-field-display-raw-data',
  range: 'dvn-field-display-range',
  reference: 'dvn-field-display-reference',
};
const 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;
