import { r as registerInstance, c as createEvent, h, H as Host } from './index-ecdfb761.js';
import { D as DvnDataAdapter } from './DvnDataAdapter-536a3258.js';
import { D as DataApiImpl } from './dataApi-f887ca8c.js';
import { r as renderReference } from './util-214b9543.js';
import { D as DvnDataProvider } from './DvnDataProvider-a3e985b5.js';
import { g as getSessionData } from './index-199df3c1.js';
import './apiBase-6015f41f.js';
import './data-transfer-field-2f305919.js';
import './BaseField-0b01dc2c.js';
import './filter-cbaa3d7d.js';
import './list-fetch-state-e772aa74.js';
import './data-model-88af49fc.js';
import './kimlik-api-fddeac81.js';
import './cookie-5d9f5819.js';
import './_commonjsHelpers-27f122dd.js';

const dvnFieldInputCellReferenceCss = "dvn-field-input-cell-reference{display:-ms-flexbox;display:flex;overflow:auto;padding:0 4px}";

// TODO remove this?
const dataApiEndpoint = () => {
  let env = 'prod';
  let base = 'mentornity';
  if (window.location.hostname === 'console.qa-one.mentornity.com') {
    env = 'qa';
    base = 'mentornity-qa';
  }
  return `/api-data/${env}/${base}/`;
};
const DvnFieldInputCellReference = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.cellFormCancel = createEvent(this, "cellFormCancel", 7);
    this.cellFormSubmit = createEvent(this, "cellFormSubmit", 7);
  }
  onAction(event) {
    if (event.detail.name === 'dvn-field-input-cell-reference.remove-item') {
      event.stopImmediatePropagation();
      const itemToRemove = event.detail.params.item;
      this.internalValue = this.internalValue.filter(item => item._id !== itemToRemove._id);
    }
  }
  componentWillLoad() {
    this.internalValue = this.value || [];
    this.dataProvider = getDataProvider(this.field);
  }
  render() {
    const { field, internalValue, meta } = this;
    const value = this.value || [];
    return (h(Host, null, Array.isArray(value)
      ? value.map(data => renderReference(field, data, meta))
      : renderReference(field, value, meta), h("dvn-dialog", { shown: true, onDismiss: () => this.cellFormCancel.emit() }, h("dvn-select", { dataAdapter: new DvnDataAdapter(field.titleField, '_id'), dataProvider: this.dataProvider, onChange: (e) => {
        var _a, _b;
        e.stopImmediatePropagation();
        const item = (_b = (_a = this.dataProvider.list) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.find(item => item._id === e.detail);
        this.internalValue = [...this.internalValue, item];
      }, leaned: true, width: "100%", value: [] }), h("dvn-list-view-table", { config: {
        hideHeader: true,
        separation: 1,
        actions: [{
            ui: {
              name: 'dvn-field-input-cell-reference.remove-item',
              buttonType: 'icon-button',
              iconName: 'uil uil-trash-alt',
              tint: 'danger',
              view: 'flat'
            }
          }],
        fields: [
          { fieldId: field.imageField },
          { fieldId: field.titleField },
        ],
        columns: {
          [field.imageField]: {
            width: 50,
          },
        },
      }, fields: field.fields, items: internalValue, isEndReached: true }), h("div", { slot: "actions" }, h("dvn-button", { view: "flat", onClick: (e) => {
        e.stopImmediatePropagation();
        this.cellFormCancel.emit();
      } }, "Cancel"), h("dvn-button", { tint: "primary", view: "flat", onClick: (e) => {
        e.stopImmediatePropagation();
        this.cellFormSubmit.emit({
          value: this.internalValue,
          apiValue: this.internalValue.map(item => convertToRef(field, item)),
        });
      } }, "Save")))));
  }
};
function getDataProvider(field) {
  const dataApi = new DataApiImpl(dataApiEndpoint(), { Authorization: `Bearer ${getSessionData().token}` });
  return new DvnDataProvider(dataApi, field.otherCollection, {
    fields: field.fields,
    sort: [{ id: field.titleField, order: 1 }]
  }, undefined, {
    fields: [
      { fieldId: field.imageField },
      { fieldId: field.titleField },
    ]
  });
}
function convertToRef(field, { _id }) {
  return {
    _ref: field.otherCollection,
    _id,
  };
}
DvnFieldInputCellReference.style = dvnFieldInputCellReferenceCss;

export { DvnFieldInputCellReference as dvn_field_input_cell_reference };
