'use strict';

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

const index = require('./index-1141a245.js');
const DvnDataAdapter = require('./DvnDataAdapter-8f89f436.js');
const dataApi = require('./dataApi-27259c95.js');
const util = require('./util-9ec42ae0.js');
const DvnDataProvider = require('./DvnDataProvider-d7268155.js');
const index$1 = require('./index-36cc8bab.js');
require('./apiBase-6c66ff1b.js');
require('./data-transfer-field-e8417d5f.js');
require('./BaseField-381327a9.js');
require('./filter-d5040f65.js');
require('./list-fetch-state-6f8f3bf8.js');
require('./data-model-01c368bc.js');
require('./kimlik-api-663f4884.js');
require('./cookie-e64bc800.js');
require('./_commonjsHelpers-206db00d.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) {
    index.registerInstance(this, hostRef);
    this.cellFormCancel = index.createEvent(this, "cellFormCancel", 7);
    this.cellFormSubmit = index.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 (index.h(index.Host, null, Array.isArray(value)
      ? value.map(data => util.renderReference(field, data, meta))
      : util.renderReference(field, value, meta), index.h("dvn-dialog", { shown: true, onDismiss: () => this.cellFormCancel.emit() }, index.h("dvn-select", { dataAdapter: new DvnDataAdapter.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: [] }), index.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 }), index.h("div", { slot: "actions" }, index.h("dvn-button", { view: "flat", onClick: (e) => {
        e.stopImmediatePropagation();
        this.cellFormCancel.emit();
      } }, "Cancel"), index.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$1 = new dataApi.DataApiImpl(dataApiEndpoint(), { Authorization: `Bearer ${index$1.getSessionData().token}` });
  return new DvnDataProvider.DvnDataProvider(dataApi$1, 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;

exports.dvn_field_input_cell_reference = DvnFieldInputCellReference;
