'use strict';

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

const index = require('./index-ce58ddc5.js');
const DvnDataAdapter = require('./DvnDataAdapter-8f89f436.js');
const dataApi = require('./dataApi-fbe22059.js');
const util = require('./util-d34d8f52.js');
const env = require('./env-83ebcbc7.js');
const index$1 = require('./index-4b96acc9.js');
require('./apiBase-b14a8285.js');
require('./data-transfer-field-0ce05c1a.js');
require('./BaseField-d019ade6.js');
require('./filter-d5040f65.js');
require('./list-fetch-state-a9e880b1.js');
require('./data-model-9afb7f54.js');
require('./kimlikApi-f4f62100.js');
require('./cookie-ea977378.js');
require('./_commonjsHelpers-206db00d.js');

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

let 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", { clip: false, shown: true, onDismiss: () => this.cellFormCancel.emit() }, 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("br", null), 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("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(env.dataApiEndpoint, { Authorization: `Bearer ${index$1.getSessionData().token}` });
  return new env.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;
