import { r as t, h as e, H as s, a as i } from "./p-9b9ccd0c.js";

import { L as n } from "./p-311eedf3.js";

import { g as a } from "./p-d42e842b.js";

import "./p-6534555e.js";

import "lit";

import "lit/directives/when.js";

import "lit/directives/if-defined.js";

const o = "property";

const r = "object";

const getPropertyIcon = (t, e) => 
// The object icon has to be retrieved from the ObjectTypes array, by
// matching the ObjectData typeId with the ObjectType id
e.find((e => e.id === t)).icon;

const convertObjectTypeToTreeItem = t => ({
  caption: t.name,
  id: t.id,
  startImgSrc: t.icon,
  leaf: false,
  parts: "object-type tree-view-item",
  expanded: true,
  items: []
});

const convertObjectToTreeItem = (t, e) => ({
  caption: t.name,
  id: t.id,
  startImgSrc: e.get(t.typeId).icon,
  parts: "object tree-view-item",
  metadata: r,
  leaf: true
});

const convertPropertyToTreeItem = (t, e) => ({
  caption: t.name,
  id: t.id,
  startImgSrc: getPropertyIcon(t.typeId, e),
  parts: "property tree-view-item",
  metadata: o,
  leaf: true
})
/**
 * Updates the tree model with the provided objects.
 * Objects may already exist and are placed under their parent node.
 * If the parent node doesn't exist (identified by typeId on ObjectTypes),
 * it is created and added at the bottom.
 *
 * @param {ObjectData[] | undefined} objects - The array of objects to update.
 * @param {Map<string, ObjectType>} objectTypesMap - A map of object types by ID.
 * @param {Set<string>} objectsSet - A set of existing object IDs.
 * @param {TreeViewModel} actualTreeState - The current tree state to be updated.
 * @returns {TreeViewModel} - The updated tree view model.
 */;

const updateObjectsTreeModel = (t, e, s, i) => {
  if (!t) {
    return [ ...i ];
  }
  const n = [ ...i ];
  t.forEach((t => {
    // objects that already exists in the actual actualTreeState should not be added.
    if (s.has(t.id)) {
      // object already exists
      return;
    }
    const i = convertObjectToTreeItem(t, e);
    // TODO: Improve the algorithm efficiency. This check visits potentially all nodes
    // from the TreeViewModel on each iteration. Using a Map to pre-store the TreeView
    // nodes would improve efficiency.
        const a = n.findIndex((e => e.id === t.typeId));
    if (a === -1) {
      // Create and add new object type tree item
      const s = e.get(t.typeId);
      if (s) {
        const t = convertObjectTypeToTreeItem(s);
        t.items.push(i);
        n.push(t);
      }
    } else {
      // objectType exists already. insert object as children of the object type tree item.
      n[a].items.push(i);
    }
    s.add(t.id);
  }));
  return n;
};

/**
 * Converts an array of KB properties data into tree item data for a tree view.
 *
 * @param {KBPropertyData[]} properties - The array of KB property data.
 * @param {KBPropertyType[]} kbPropertiesTypes - The array of KB property types.
 * @param {TreeViewModel} actualTreeState - The current state of the tree.
 * @returns {TreeViewModel} - The updated tree view item models.
 */ const convertKbPropertiesDataToTreeItemData = (t, e, s) => {
  if (!t) {
    return [ ...s ];
  }
  const i = [ ...s ];
  t.forEach((t => {
    // properties have no parent node, and they should be added at the beginning of the tree
    const s = i.find((e => e.id === t.id)) !== undefined;
    if (s) {
      return;
    }
    const n = convertPropertyToTreeItem(t, e);
    i.unshift(n);
  }));
  return i;
};

/**
 * Removes properties from the tree model.
 *
 * @param {TreeViewModel} actualTreeState - The current state of the tree,
 * containing various tree view items.
 * @returns {TreeViewModel} - A new array of tree view items that excludes
 * any items with metadata of type `KB_PROPERTY`.
 */ const clearKbPropertiesFromTree = t => t.filter((t => t.metadata !== o));

const c = 'button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;grid-template-rows:max-content 1fr max-content}section{display:contents}.header__field-group{grid-template:"export-file-name buttons-container" max-content "checkboxes checkboxes" max-content/1fr max-content}.export-file-name{grid-area:export-file-name}.header__buttons-container{grid-area:buttons-container}.header__checkboxes{grid-area:checkboxes;grid-template-columns:max-content max-content}.main{position:relative;padding-block:12px}';

var h = undefined && undefined.__classPrivateFieldGet || function(t, e, s, i) {
  if (s === "a" && !i) throw new TypeError("Private accessor was defined without a getter");
  if (typeof e === "function" ? t !== e || !i : !e.has(t)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return s === "m" ? i : s === "a" ? i.call(t) : i ? i.value : e.get(t);
};

var d = undefined && undefined.__classPrivateFieldSet || function(t, e, s, i, n) {
  if (i === "m") throw new TypeError("Private method is not writable");
  if (i === "a" && !n) throw new TypeError("Private accessor was defined without a setter");
  if (typeof e === "function" ? t !== e || !n : !e.has(t)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return i === "a" ? n.call(t, s) : n ? n.value = s : e.set(t, s), s;
};

var l, f, p, u, b, m, w, y, k, g, x, v, M, W, T, j, C, _, I, S, E, z, V, L;

const A = a({
  category: "gemini-tools",
  name: "file",
  colorType: "on-surface"
});

const D = a({
  category: "gemini-tools",
  name: "settings",
  colorType: "primary"
});

const P = a({
  category: "window-tools",
  name: "references",
  colorType: "interactive"
});

const F = a({
  category: "menus",
  name: "new-object",
  colorType: "on-primary"
});

const G = a({
  category: "controls",
  name: "treeview"
});

const H = "true";

const K = [ "resets/box-sizing", "components/button", "components/checkbox", "components/edit", "components/icon", "components/tree-view", "utils/form", "utils/layout", "utils/spacing", "utils/typography" ];

const q = class {
  constructor(s) {
    t(this, s);
    l.add(this);
    f.set(this, void 0);
    p.set(this, void 0);
    u.set(this, false);
    b.set(this, void 0);
    m.set(this, void 0);
    w.set(this, void 0);
    /**
         * Stores the object types information (name, and icon)
         */
    // eslint-disable-next-line @stencil-community/own-props-must-be-private
        y.set(this, new Map);
    /**
         * Stores the object's id that have been added.
         */
    // eslint-disable-next-line @stencil-community/own-props-must-be-private
        k.set(this, new Set);
    g.set(this, (async t => {
      this.addKbPropertiesIsChecked = h(this, f, "f").value === H;
      if (this.addKbPropertiesIsChecked) {
        d(this, w, await this.addKBPropertiesCallback(), "f");
        if (h(this, w, "f").length) {
          this.objectsTreeViewModel = convertKbPropertiesDataToTreeItemData(h(this, w, "f"), this.kbPropertiesTypes, this.objectsTreeViewModel);
        }
      } else {
        // previous properties (if any) should be removed
        this.objectsTreeViewModel = clearKbPropertiesFromTree(this.objectsTreeViewModel);
      }
    }));
    x.set(this, (async () => {
      h(this, b, "f").value = undefined;
      this.exportAllIsChecked = false;
      const t = await this.addObjectsCallback();
      h(this, L, "f").call(this, t);
    }));
    v.set(this, (async () => {
      const t = await this.addReferencesCallback(this.checkedObjectsIds);
      h(this, L, "f").call(this, t);
    }));
    M.set(this, (async () => {
      const t = await this.cancelCallback();
      if (t) {
        this.exportingIsInProcess = false;
      }
    }));
    W.set(this, (() => {
      this.objectsTreeViewModel = [];
      h(this, k, "f").clear();
      h(this, f, "f").value = undefined;
      h(this, b, "f").value = undefined;
    }));
    T.set(this, (() => {
      // Nothing to display on the tree-view
      if (!this.hasObjects && !this.exportAllIsChecked) {
        return e("gx-ide-empty-state", {
          isAnimated: h(this, u, "f"),
          stateTitle: h(this, p, "f").main.noObjectsToDisplay,
          key: "no-objects-empty-state"
        }, e("button", {
          class: "button-primary",
          onClick: h(this, x, "f")
        }, h(this, p, "f").main.beginByADdingSomeObjects));
      } else {
        d(this, u, true, "f");
 // improve performance if is first render
            }
      if (this.exportAllIsChecked) {
        return e("gx-ide-empty-state", {
          isAnimated: h(this, u, "f"),
          stateIconSrc: G,
          stateTitle: h(this, p, "f").main.exportAllMessage,
          key: "exporting-all-empty-state"
        });
      } else {
        d(this, u, true, "f");
 // improve performance if is first render
            }
      return e("ch-tree-view-render", {
        class: "tree-view",
        checkbox: true,
        checked: true,
        dragDisabled: true,
        dropDisabled: true,
        filter: h(this, C, "f").call(this),
        filterType: this.addKbPropertiesIsChecked && !this.exportAllIsChecked ? "none" : "metadata",
        model: this.objectsTreeViewModel,
        showLines: "last",
        toggleCheckboxes: true,
        sortItemsCallback: h(this, z, "f"),
        onCheckedItemsChange: h(this, E, "f")
      });
    }));
    j.set(this, (() => {
      this.hasObjects = this.objectsTreeViewModel && this.objectsTreeViewModel.length > 0;
    }));
    C.set(this, (() => {
      if (this.addKbPropertiesIsChecked && this.exportAllIsChecked) {
        return o;
 // only show properties
            }
      if (!this.addKbPropertiesIsChecked && !this.exportAllIsChecked) {
        return r;
 // only show objects
            }
      return "none";
    }));
    _.set(this, (t => {
      this.exportAllIsChecked = h(this, b, "f").value === H;
    }));
    I.set(this, (async () => {
      this.exportingIsInProcess = true;
      const t = h(this, m, "f").value;
      const e = this.addKbPropertiesIsChecked ? this.checkedPropertiesIds : [];
      const s = this.exportAllIsChecked ? undefined : this.checkedObjectsIds;
      this.exportCallback(t, e, s).then((() => {
        this.exportingIsInProcess = false;
      }));
      // returns Promise<boolean> but no procedure was detailed on the spec.
        }));
    E.set(this, (t => {
      // filter 'checked objects' and 'checked properties' ids
      const e = [];
      const s = [];
      const i = [ ...t.detail.values() ];
      i.forEach((t => {
        if (t.item.checked && t.item.metadata === r) {
          e.push(t.item.id);
        } else if (t.item.checked && t.item.metadata === o) {
          s.push(t.item.id);
        }
      }));
      this.checkedObjectsIds = e;
      this.checkedPropertiesIds = s;
      this.atLeastOnePropertyItemIsChecked = this.checkedPropertiesIds.length > 0;
      this.atLeastOneObjectItemIsChecked = this.checkedObjectsIds.length > 0;
    }));
    z.set(this, (t => t.sort(((t, e) => {
      if (t.metadata === "property" && e.metadata !== "property") {
        return -1;
      }
      if (e.metadata === "property" && t.metadata !== "property") {
        return 1;
      }
      return t.caption.localeCompare(e.caption);
    }))));
    V.set(this, (() => {
      const t = this.atLeastOneObjectItemIsChecked;
      const s = this.objectsTreeViewModel.length > 0;
      return e("footer", {
        class: "footer dialog-footer-with-border dialog-footer-space-between space-body spacing-body-inline spacing-body-block-end"
      }, e("button", {
        class: "button-tertiary clear-btn",
        disabled: !s,
        part: "clear-button",
        onClick: s && h(this, W, "f")
      }, h(this, p, "f").footer.clearButton), e("div", {
        class: "buttons-spacer"
      }, e("button", {
        class: "button-secondary button-icon-and-text",
        part: "references-button",
        disabled: !t,
        onClick: t && h(this, v, "f")
      }, e("ch-image", {
        class: "icon-m",
        disabled: !t,
        src: P
      }), h(this, p, "f").footer.addReferencesButton), e("button", {
        // Add button
        class: "button-primary button-icon-and-text",
        disabled: this.exportAllIsChecked,
        part: "add-button",
        onClick: !this.exportAllIsChecked && h(this, x, "f")
      }, e("ch-image", {
        class: "icon-m",
        disabled: this.exportAllIsChecked,
        src: F
      }), h(this, p, "f").footer.addButton)));
    }));
    L.set(this, (t => {
      if (t.length) {
        const e = updateObjectsTreeModel(t, h(this, y, "f"), h(this, k, "f"), this.objectsTreeViewModel);
        this.objectsTreeViewModel = e;
      }
    }));
    this.addKbPropertiesIsChecked = false;
    this.atLeastOnePropertyItemIsChecked = false;
    this.atLeastOneObjectItemIsChecked = false;
    this.checkedObjectsIds = [];
    this.checkedPropertiesIds = [];
    this.exportAllIsChecked = false;
    this.exportingIsInProcess = false;
    this.hasObjects = undefined;
    this.objectsTreeViewModel = [];
    this.addKBPropertiesCallback = undefined;
    this.addObjectsCallback = undefined;
    this.addReferencesCallback = undefined;
    this.cancelCallback = undefined;
    this.exportCallback = undefined;
    this.exportFileDirectoryCallback = undefined;
    this.fileName = undefined;
    this.kbPropertiesTypes = undefined;
    this.loader = false;
    this.objectTypes = undefined;
    this.optionsCallback = undefined;
  }
  objectsTreeViewModelChanged(t) {
    this.hasObjects = t && t.length > 0;
    if (!this.hasObjects) {
      this.atLeastOnePropertyItemIsChecked = false;
      this.atLeastOneObjectItemIsChecked = false;
    }
  }
  objectTypesChanged(t) {
    h(this, l, "m", S).call(this, t);
  }
  async componentWillLoad() {
    d(this, p, await n.getComponentStrings(this.el), "f");
    h(this, j, "f").call(this);
    h(this, l, "m", S).call(this, this.objectTypes);
  }
  render() {
    const t = this.atLeastOnePropertyItemIsChecked || this.atLeastOneObjectItemIsChecked || this.exportAllIsChecked;
    const i = this.exportingIsInProcess;
    return e(s, {
      class: "widget"
    }, e("ch-theme", {
      model: K
    }), e("section", null, e("header", {
      class: "header dialog-header-with-border space-body spacing-body-inline spacing-body-block-start"
    }, e("div", {
      class: "field-group header__field-group"
    }, e("div", {
      class: "field field-inline export-file-name"
    }, e("label", {
      class: "label",
      htmlFor: "xpz-file"
    }, h(this, p, "f").header.exportFileName), e("ch-edit", {
      autoFocus: true,
      id: "xpz-file",
      class: "input",
      part: "xpz-file",
      value: this.fileName,
      startImgSrc: A,
      ref: t => d(this, m, t, "f")
    })), e("div", {
      class: "header__buttons-container buttons-spacer"
    }, e("button", {
      id: "export-kb-btn",
      class: "button-primary export-btn",
      part: "export-btn",
      disabled: !t,
      onClick: t && h(this, I, "f")
    }, h(this, p, "f").header.exportButton), e("button", {
      id: "cancel-kb-export-btn",
      class: "button-secondary cancel-btn",
      part: "cancel-button",
      disabled: !i,
      onClick: i && h(this, M, "f")
    }, h(this, p, "f").header.cancelButton), e("button", {
      "aria-label": h(this, p, "f").header.optionsButton,
      title: h(this, p, "f").header.optionsButton,
      class: "button-tertiary button-icon-only options-btn",
      part: "select-kb-btn",
      onClick: this.optionsCallback
    }, e("ch-image", {
      class: "icon-m",
      src: D
    }))), e("div", {
      class: "header__checkboxes field-group"
    }, e("ch-checkbox", {
      class: "checkbox add-kb-description",
      part: "add-kb-description",
      caption: h(this, p, "f").main.addKnowledgeBaseDescription,
      checkedValue: H,
      onInput: h(this, g, "f"),
      ref: t => d(this, f, t, "f")
    }), e("ch-checkbox", {
      class: "checkbox",
      part: "export-all-checkbox",
      checkedValue: H,
      caption: h(this, p, "f").main.exportAll,
      onInput: h(this, _, "f"),
      ref: t => d(this, b, t, "f")
    })))), e("div", {
      class: "main spacing-body-inline"
    }, h(this, T, "f").call(this), this.loader && this.exportingIsInProcess && e("gx-ide-loader", {
      cancelLabel: h(this, p, "f").loader.cancelLabel,
      loaderTitle: h(this, p, "f").loader.title,
      cancelCallback: h(this, M, "f"),
      show: true,
      class: "loader"
    })), h(this, V, "f").call(this)));
  }
  static get assetsDirs() {
    return [ "gx-ide-assets/kb-manager-export" ];
  }
  get el() {
    return i(this);
  }
  static get watchers() {
    return {
      objectsTreeViewModel: [ "objectsTreeViewModelChanged" ],
      objectTypes: [ "objectTypesChanged" ]
    };
  }
};

f = new WeakMap, p = new WeakMap, u = new WeakMap, b = new WeakMap, m = new WeakMap, 
w = new WeakMap, y = new WeakMap, k = new WeakMap, g = new WeakMap, x = new WeakMap, 
v = new WeakMap, M = new WeakMap, W = new WeakMap, T = new WeakMap, j = new WeakMap, 
C = new WeakMap, _ = new WeakMap, I = new WeakMap, E = new WeakMap, z = new WeakMap, 
V = new WeakMap, L = new WeakMap, l = new WeakSet, S = function _GxIdeKbManagerExport_objectsTypesArrayToMap(t) {
  t.forEach((t => {
    h(this, y, "f").set(t.id, t);
  }));
};

q.style = c;

export { q as gx_ide_kb_manager_export };
//# sourceMappingURL=p-9ca0c042.entry.js.map