import { r as registerInstance, c as createEvent, h, H as Host } from './index-102043e0.js';
import { a as generateActionComponent } from './action-util-2de171dc.js';
import { T as TitlePosition, I as ImagePosition } from './models-22fcdeab.js';
import { f as findField } from './field-58e26895.js';
import { a as getDataModelFieldDisplayValue } from './data-model-f5160369.js';
import { N as NestedField } from './nested-395a4cd4.js';
import './index-336ef773.js';
import './BaseField-461b6d0c.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;
  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) || [],
    }
  };
}

let DvnCardView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.action = 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 (h(Host, { style: this.overrideStyle }, h("dvn-card", { cornerRadius: this.cornerRadius, elevation: 0, leaned: this.leaned, style: overrideStyle }, showHeader &&
      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 &&
        h("div", { class: "header-actions-container", slot: "action" }, cardItem.header.actions.map((action, index) => generateActionComponent(index, {}, item, this.action, action)))), cardItem.body.fields.map(field => {
      if (field.customViewTag) {
        const Tag = field.customViewTag;
        return (h(Tag, { item: item, field: field }));
      }
      else {
        return (h("dvn-field-display", { infoTitle: field.label, field: field.field, value: field.value, meta: field.meta }));
      }
    }), cardItem.footer.actions.length > 0 &&
      h("dvn-card-footer", { slot: "footer", alignRight: true }, cardItem.footer.actions.map((action, index) => 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) {
    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 === NestedField.type) {
      Tag = fieldTagMap[field.nestedField.type];
      fieldToPass = field.nestedField;
    }
    return (h(Host, null, showTitle && h("div", { class: "title" }, (infoTitle || '').toUpperCase()), Tag
      ? h(Tag, { field: fieldToPass, value: value, meta: meta, fixHeight: false })
      : h("span", null, value)));
  }
};
DvnFieldDisplay.style = dvnFieldDisplayCss;

export { DvnCardView as dvn_card_view, DvnFieldDisplay as dvn_field_display };
