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

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

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

import "lit";

import "lit/directives/when.js";

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

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

const r = 30;

/* objects in file metadata*/ const c = "object";

const h = "category";

const l = "status";

// - - - - - - - - - - - - - - - - -
// Objects In File Tree Model
// - - - - - - - - - - - - - - - - -
const convertImportCategoryItemToTreeItem = (t, e) => {
  const i = [];
  e.forEach((e => {
    i.push({
      id: e.id,
      caption: e.name,
      expanded: true,
      leaf: true,
      startImgSrc: t,
      metadata: c
    });
  }));
  return i;
};

const convertImportCategoryDataToTreeView = t => {
  const e = [];
  if (!t) {
    return e;
  }
  t.forEach((t => {
    const i = {
      id: t.id,
      caption: `${t.name} (${t.items.length})`,
      expanded: t.items.length <= r,
      lazy: t.items.length > r,
      leaf: false,
      startImgSrc: t.icon,
      metadata: h,
      items: t.items.length <= r ? convertImportCategoryItemToTreeItem(t.icon, t.items) : []
    };
    e.push(i);
  }));
  return e;
};

// - - - - - - - - - - - - - - - - -
// Import Status Tree Model
// - - - - - - - - - - - - - - - - -
const createImportResultDataMessages = (t, e, i) => {
  const s = [];
  e.forEach(((e, a) => {
    s.push({
      id: `${t}-message-${a}`,
      caption: e,
      startImgSrc: "gemini-tools/list-view/on-surface",
      leaf: true,
      metadata: JSON.stringify({
        [l]: i
      })
    });
  }));
  return s;
};

const createImportResultItem = (t, e) => {
  const i = `${e.id}-${e.name}`;
  const s = {
    id: i,
    startImgSrc: t.icon,
    caption: e.name,
    parts: `tree-view-item tree-view-item--status tree-view-item--${e.status}`,
    expanded: true,
    leaf: e.messages.length === 0,
    metadata: JSON.stringify({
      [l]: e.status
    }),
    items: createImportResultDataMessages(i, e.messages, e.status)
  };
  return s;
};

// This functions updates the "Import Status" model, by adding a new result item.
const addImportItemResultDataItem = (t, e, i, s) => {
  const a = [ ...s ];
  // Use the resultItem.id to find the corresponding category in the categoryMap
    const n = i.get(t.id);
  const o = e.get(n);
  const r = a.findIndex((t => t.id === n));
  if (r === -1) {
    // category does not exists yet.
    const e = {
      id: n,
      caption: `${o.name} (1)`,
      startImgSrc: `${o.icon}`,
      expanded: true,
      items: [ createImportResultItem(o, t) ]
    };
    a.push(e);
    return a;
  }
  // else category already exists. Just insert the result item in the category.
    const c = createImportResultItem(o, t);
  a[r].items = [ ...a[r].items, c ];
  // update category caption length
    const h = a[r].items.length;
  a[r].caption = `${o.name} (${h})`;
  return a;
};

const d = 'button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;grid-template:"header header" max-content "objects-in-file import-status" 1fr "footer footer" max-content;grid-template-columns:1fr 1fr}.section{display:contents}.header{grid-template-columns:1fr max-content;grid-area:header}.objects-in-file__section{grid-area:objects-in-file}.objects-in-file__main{border-inline-end:var(--section-common-border);grid-template-rows:1fr max-content}.objects-in-file__footer{display:grid}.import-status__section{grid-area:import-status}.import-status__main{grid-template-rows:1fr max-content}.import-status__footer{display:flex}.import-clear-status-btn{margin-inline-start:auto}.objects-in-file__section,.import-status__section{display:grid;grid-template-rows:max-content 1fr}.dialog-header__objects-in-file,.dialog-header__import-status{padding-block-start:16px}.objects-in-file__main,.import-status__main{grid-auto-rows:max-content;padding-block:12px;overflow:auto}.footer{display:flex;grid-area:footer;border-block-start:var(--section-common-border)}.text-align-center{text-align:center}.tree-view::part(item__action tree-view-item--status){grid-template:"first-img left-img text right-img"/max-content max-content 1fr max-content}.tree-view::part(item__action tree-view-item--status)::before{grid-area:first-img;content:"";width:8px;height:8px;border-radius:50%;background-color:var(--gray-03);margin-inline-end:2px}.tree-view::part(item__action tree-view-item--success):before{background-image:none;background-color:var(--color-success-dark)}.tree-view::part(item__action tree-view-item--warning):before{background-image:none;background-color:var(--color-warning-dark)}.tree-view::part(item__action tree-view-item--error):before{background-image:none;background-color:var(--color-error-dark)}.tree-view-import::part(item){content-visibility:auto;contain-intrinsic-size:auto 22px}';

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

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

var m, u, b, g, w, k, y, v, _, x, M, W, j, C, T, S, I, $, E, D, L, z, F, O, A, H, R, N;

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

const J = n({
  category: "gemini-tools",
  name: "notice",
  colorType: "on-surface"
});

const V = n({
  category: "gemini-tools",
  name: "file",
  colorType: "primary"
});

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

const X = n({
  category: "menus",
  name: "delete",
  colorType: "primary"
});

const Y = "allChecked";

const q = "allUnchecked";

const B = class {
  constructor(i) {
    t(this, i);
    this.componentDidLoadEvent = e(this, "componentDidLoadEvent", 7);
    this.componentDidRenderFirstTime = e(this, "componentDidRenderFirstTime", 7);
    /**
         * The component hard-coded strings translations.
         */
    // eslint-disable-next-line @stencil-community/own-props-must-be-private
        m.set(this, void 0);
    u.set(this, []);
    b.set(this, void 0);
    g.set(this, void 0);
    w.set(this, void 0);
    k.set(this, new Set);
    y.set(this, new Set);
    v.set(this, new Set);
    // #categoryItemMap maps a category id, with the name and icon.
    // required for the "import status" tree.
        _.set(this, new Map);
    x.set(this, new Map);
    M.set(this, void 0);
    W.set(this, (() => {
      this.cancelCallback();
    }));
    j.set(this, (() => {
      this.importTreeState = [];
      this.statusInfo.error.number = 0;
      this.statusInfo.warning.number = 0;
      this.statusInfo.success.number = 0;
    }));
    C.set(this, (() => {
      var t;
      this.noImport = !!(((t = this.importTreeState) === null || t === void 0 ? void 0 : t.length) === 0 || !this.importTreeState);
    }));
    T.set(this, (() => {
      this.someStatusVisible = this.statusInfo.error.display || this.statusInfo.warning.display || this.statusInfo.success.display;
    }));
    S.set(this, (async () => {
      var t;
      /* first clear imported objects (this clears errors/warnings/success counts as well)*/      f(this, j, "f").call(this);
      /* then do the import*/      this.importingIsInProcess = true;
      /*
              Process checked objects manually, considering lazy loading.
              Example:
              1. The "Attributes" category is lazy-loaded and marked as checked.
              2. The user runs the import without expanding the node.
              3. No attribute objects appear as checked, since lazy load was not triggered.
              4. Still, all attribute objects must be included, because the category itself is checked.
        
              This avoids forcing users to expand nodes just to select objects.
              Large categories are not expanded by default to improve performance, which is crucial
              for very large imports (1000+ objects).
            */      const e = new Set;
      for (const i of f(this, w, "f").model) {
        if (((t = i.items) === null || t === void 0 ? void 0 : t.length) > 0) {
          for (const t of i.items) {
            if (t.checked) {
              e.add(t.id);
            }
          }
        } else if (i.lazy && i.checked) {
          const t = f(this, M, "f").find((t => t.id == i.id));
          for (const i of t.items) {
            e.add(i.id);
          }
        }
      }
      // Compute excluded ids against the full file contents so the server's importAll branch can be used without re-reading the tree.
            const i = [];
      for (const t of f(this, M, "f")) {
        for (const s of t.items) {
          if (!e.has(s.id)) {
            i.push(s.id);
          }
        }
      }
      const s = Array.from(e);
      const a = i.length <= s.length;
      const n = {
        importAll: a,
        objectIds: a ? [] : s,
        excludedObjectIds: a ? i : []
      };
      await this.importCallback(n);
      this.importingIsInProcess = false;
    }));
    I.set(this, (async t => {
      p(this, M, await this.loadCallback(t), "f");
      f(this, _, "f").clear();
      f(this, x, "f").clear();
      f(this, M, "f").forEach((t => {
        f(this, _, "f").set(t.id, {
          name: t.name,
          icon: t.icon
        });
        t.items.forEach((e => {
          f(this, x, "f").set(e.id, t.id);
        }));
      }));
      const e = convertImportCategoryDataToTreeView(f(this, M, "f"));
      this.objectsTreeModel = e;
      // Tree-view renders with `checked={true}` by default, so a freshly
      // loaded file starts with everything selected.
            this.hasSelection = e.length > 0;
    }));
    $.set(this, (async t => {
      const e = f(this, M, "f").find((e => e.id === t));
      return e && e.items.length > 0 ? convertImportCategoryItemToTreeItem(e.icon, e.items) : [];
    }));
    E.set(this, (t => {
      const e = t.detail;
      const i = [];
      const s = [];
      e.forEach((t => {
        if (t.item.checked) {
          i.push(t.item.id);
          if (t.item.lazy && !t.item.items) s.push(t.item.id);
        }
      }));
      for (let t of s) {
        const e = f(this, M, "f").find((e => e.id === t));
        if (e) {
          for (let t of e.items) {}
        }
      }
      // Evaluate "Select All / Unselect All" checkbox state
            if (e.size === i.length) {
        this.selectAllCheckboxValue = Y;
        this.selectAllCheckboxIndeterminate = false;
      } else if (i.length === 0) {
        this.selectAllCheckboxValue = q;
        this.selectAllCheckboxIndeterminate = false;
      } else {
        this.selectAllCheckboxIndeterminate = true;
      }
      this.hasSelection = i.length > 0;
    }));
    D.set(this, (t => {
      if (t.detail.metadata === c) {
        this.objectContextMenuCallback("imported", {
          selection: f(this, u, "f"),
          clientX: t.detail.contextmenuEvent.screenX,
          clientY: t.detail.contextmenuEvent.screenY
        });
      }
    }));
    L.set(this, (t => {
      p(this, u, t.detail.map((t => t.item.id)), "f");
    }));
    z.set(this, (() => {
      if (this.optionsCallback) {
        this.optionsCallback();
        // returns boolean
            }
    }));
    F.set(this, (() => {
      this.selectedFile = null;
      f(this, g, "f").value = null;
    }));
    O.set(this, (() => {
      f(this, g, "f").click();
    }));
    A.set(this, (t => {
      var e;
      const i = t.target;
      if (((e = i.files) === null || e === void 0 ? void 0 : e.length) > 0) {
        this.selectedFile = i.files[0];
      }
    }));
    H.set(this, (t => {
      const e = t.detail;
      const i = [];
      const s = e.findIndex((t => t.itemId === "errors"));
      const a = e.findIndex((t => t.itemId === "warnings"));
      const n = e.findIndex((t => t.itemId === "successes"));
      if (s !== -1) {
        i.push("error");
      }
      if (a !== -1) {
        i.push("warning");
      }
      if (n !== -1) {
        i.push("success");
      }
      let o = `"${l}":""`;
      if (i.length) {
        const t = i.join("|");
        o = `"${l}":"(${t})"`;
      }
      this.importedTreeFilterRegExp = new RegExp(o, "i");
      // Update statusInfo for the status-buttons
            this.statusInfo = {
        error: {
          display: s !== -1,
          number: this.statusInfo.error.number
        },
        warning: {
          display: a !== -1,
          number: this.statusInfo.warning.number
        },
        success: {
          display: n !== -1,
          number: this.statusInfo.success.number
        }
      };
      f(this, T, "f").call(this);
    }));
    R.set(this, (t => {
      if (f(this, w, "f")) {
        const e = t.detail === Y;
        f(this, w, "f").updateAllItemsProperties({
          checked: e
        });
        // updateAllItemsProperties does not fire onCheckedItemsChange, so we
        // mirror the new selection state explicitly to keep the Import button
        // disabled when the user unchecks everything via this toggle.
                this.hasSelection = e && this.objectsTreeModel.length > 0;
      }
    }));
    N.set(this, (() => {
      f(this, v, "f").clear();
      f(this, y, "f").clear();
      f(this, k, "f").clear();
      this.importTreeState.forEach((t => {
        const e = t.items;
        e.forEach((t => {
          if (t.metadata === "error") {
            f(this, v, "f").add(t.id);
          }
          if (t.metadata === "warning") {
            f(this, y, "f").add(t.id);
          }
          if (t.metadata === "success") {
            f(this, k, "f").add(t.id);
          }
        }));
      }));
    }));
    this.selectAllCheckboxValue = undefined;
    this.selectAllCheckboxIndeterminate = false;
    this.hasSelection = false;
    this.importStatusMessage = undefined;
    this.importTreeState = [];
    this.importedTreeFilterRegExp = undefined;
    this.importingIsInProcess = false;
    this.importTreeFilterList = [];
    this.noImport = undefined;
    this.noObjects = true;
    this.objectsTreeModel = [];
    this.showHiddenImportsMessage = false;
    this.someStatusVisible = undefined;
    this.statusInfo = {
      error: {
        display: true,
        number: 0
      },
      warning: {
        display: true,
        number: 0
      },
      success: {
        display: true,
        number: 0
      }
    };
    this.cancelCallback = undefined;
    this.importCallback = undefined;
    this.loadCallback = undefined;
    this.objectContextMenuCallback = undefined;
    this.optionsCallback = undefined;
    this.selectedFile = undefined;
    this.statusMinimal = false;
  }
  importTreeStateChanged() {
    // this.#filterImportedHandler();
  }
  watchImportTreeStateHandler() {
    f(this, C, "f").call(this);
    if (this.noImport) {
      this.importStatusMessage = f(this, m, "f").main.importStatus.noImportedFiles;
    } else {
      this.importStatusMessage = f(this, m, "f").main.importStatus.importedButHidden;
    }
  }
  watchObjectsTreeModelHandler(t) {
    this.noObjects = !t || t.length === 0;
  }
  selectedFileChanged(t) {
    if (t) {
      f(this, I, "f").call(this, t);
    }
  }
  componentDidLoad() {
    f(this, b, "f").focus();
  }
  componentDidRender() {}
  async componentWillLoad() {
    p(this, m, await o.getComponentStrings(this.el), "f");
    this.componentDidLoadEvent.emit(true);
    // this.#evaluateObjects();
        f(this, C, "f").call(this);
    this.importStatusMessage = f(this, m, "f").main.importStatus.noImportedFiles;
    f(this, T, "f").call(this);
  }
  /**
     * Add the result of importing an item
     */  async addResultItem(t) {
    this.importTreeState = addImportItemResultDataItem(t, f(this, _, "f"), f(this, x, "f"), this.importTreeState);
    this.statusInfo[t.status].number++;
    f(this, N, "f").call(this);
  }
  render() {
    var t, e;
    return i(s, {
      class: "widget"
    }, i("ch-theme", {
      model: P
    }), i("section", {
      class: "section"
    }, i("header", {
      // header
      class: "field-group dialog-header-with-border header header-two-cols spacing-body-block-start spacing-body-inline",
      slot: "header"
    }, i("div", {
      // select a file
      class: "field field-inline"
    }, i("label", {
      class: "label",
      htmlFor: "file-name"
    }, f(this, m, "f").header.fileNameLabel), i("ch-edit", {
      autoFocus: true,
      id: "file-name",
      class: "input",
      part: "file-name",
      type: "text",
      readonly: true,
      value: (t = this.selectedFile) === null || t === void 0 ? void 0 : t.name,
      placeholder: f(this, m, "f").header.fileNamePlaceholder,
      startImgSrc: V,
      ref: t => p(this, b, t, "f")
    }), i("input", {
      hidden: true,
      type: "file",
      accept: ".xpz, .xml",
      onChange: f(this, A, "f"),
      ref: t => p(this, g, t, "f")
    })), i("div", {
      class: "buttons-spacer"
    }, i("button", {
      "aria-label": f(this, m, "f").header.removeFileSelection,
      title: f(this, m, "f").header.removeFileSelection,
      id: "reset-all-button",
      class: "button-tertiary button-icon-only",
      part: "reset-all-button",
      onClick: f(this, F, "f")
    }, i("ch-image", {
      class: "icon-m",
      src: X
    })), i("button", {
      // select file
      id: "select-file-load-button",
      class: "button-primary",
      part: "select-file-load-button",
      onClick: f(this, O, "f"),
      disabled: this.importingIsInProcess
    }, f(this, m, "f").header.selectFileButton))), i("section", {
      // objects in file
      class: "objects-in-file__section"
    }, i("h2", {
      class: "dialog-header dialog-header-with-border dialog-header__objects-in-file subtitle-regular-xs\t text-align-center spacing-body-inline"
    }, f(this, m, "f").main.objectsInFile.title), i("div", {
      class: "field-group objects-in-file__main"
    }, !this.noObjects ? i("ch-tree-view-render", {
      // objects in file tree
      showLines: "last",
      class: "tree-view tree-view-import",
      model: this.objectsTreeModel,
      dragDisabled: true,
      dropDisabled: true,
      toggleCheckboxes: true,
      checkbox: true,
      checked: true,
      lazyLoadTreeItemsCallback: f(this, $, "f"),
      onCheckedItemsChange: f(this, E, "f"),
      onSelectedItemsChange: f(this, L, "f"),
      onItemContextmenu: f(this, D, "f"),
      ref: t => p(this, w, t, "f")
    }) : i("gx-ide-empty-state", {
      isAnimated: false,
      stateTitle: f(this, m, "f").main.objectsInFile.noObjects,
      key: "no-objects-empty-state"
    }, i("button", {
      // begin by selecting a file
      class: "button-secondary",
      onClick: f(this, O, "f")
    }, f(this, m, "f").main.objectsInFile.selectFile)), i("footer", {
      class: "field-group objects-in-file__footer spacing-body-inline"
    }, i("ch-checkbox", {
      // select all / unselect all checkbox
      id: "select-all-checkbox",
      class: "checkbox create-data-in-kb",
      checkedValue: Y,
      unCheckedValue: q,
      caption: f(this, m, "f").main.objectsInFile.selectUnselect,
      value: this.selectAllCheckboxValue,
      onInput: f(this, R, "f"),
      disabled: this.importingIsInProcess || this.objectsTreeModel.length === 0,
      part: "select-all-checkbox"
    }), i("div", {
      class: "buttons-spacer"
    }, i("button", {
      // import objects button
      id: "import-btn",
      class: "button-primary ",
      onClick: f(this, S, "f"),
      disabled: ((e = this.objectsTreeModel) === null || e === void 0 ? void 0 : e.length) === 0 || this.importingIsInProcess || !this.hasSelection
    }, f(this, m, "f").main.objectsInFile.importButton), i("button", {
      // cancel-import button
      id: "cancel-import-btn",
      class: "button-secondary ",
      onClick: f(this, W, "f"),
      disabled: !this.importingIsInProcess,
      part: "cancel-import-button"
    }, f(this, m, "f").main.objectsInFile.cancelButton), i("button", {
      // settings/options button
      id: "select-kb-btn",
      class: "button-tertiary button-icon-only",
      part: "select-kb-btn",
      onClick: f(this, z, "f")
    }, i("ch-image", {
      class: "icon-m",
      src: U
    })))))), i("section", {
      // import status
      class: "import-status__section"
    }, i("h2", {
      class: "dialog-header dialog-header-with-border dialog-header__import-status subtitle-regular-xs\t text-align-center spacing-body-inline"
    }, f(this, m, "f").main.importStatus.title), i("div", {
      class: "field-group import-status__main"
    }, !this.noImport && this.someStatusVisible ? i("ch-tree-view-render", {
      // import status tree
      model: this.importTreeState,
      toggleCheckboxes: true,
      showLines: "last",
      class: "tree-view tree-view-import",
      filter: this.importedTreeFilterRegExp,
      filterType: "metadata",
      filterOptions: {
        autoExpand: true,
        hideMatchesAndShowNonMatches: false
      }
    }) : i("gx-ide-empty-state", {
      isAnimated: false,
      stateIconSrc: J,
      stateTitle: this.importStatusMessage,
      key: "no-objects-empty-state"
    }), i("footer", {
      class: "import-status__footer spacing-body-inline"
    }, i("button", {
      // clear status button
      class: "button-secondary import-clear-status-btn",
      onClick: f(this, j, "f"),
      disabled: this.noImport || this.importingIsInProcess,
      part: "clear-status-btn"
    }, f(this, m, "f").main.importStatus.clearButton)))), i("footer", {
      // footer
      class: "footer"
    }, i("gx-ide-status-buttons", {
      errors: this.statusInfo.error.number,
      warnings: this.statusInfo.warning.number,
      successes: this.statusInfo.success.number,
      hideMessage: true,
      compact: true,
      minimal: this.statusMinimal,
      onSelectionChanged: f(this, H, "f")
    }))));
  }
  static get assetsDirs() {
    return [ "gx-ide-assets/kb-manager-import" ];
  }
  get el() {
    return a(this);
  }
  static get watchers() {
    return {
      importTreeState: [ "importTreeStateChanged", "watchImportTreeStateHandler" ],
      objectsTreeModel: [ "watchObjectsTreeModelHandler" ],
      selectedFile: [ "selectedFileChanged" ]
    };
  }
};

m = new WeakMap, u = new WeakMap, b = new WeakMap, g = new WeakMap, w = new WeakMap, 
k = new WeakMap, y = new WeakMap, v = new WeakMap, _ = new WeakMap, x = new WeakMap, 
M = new WeakMap, W = new WeakMap, j = new WeakMap, C = new WeakMap, T = new WeakMap, 
S = new WeakMap, I = new WeakMap, $ = new WeakMap, E = new WeakMap, D = new WeakMap, 
L = new WeakMap, z = new WeakMap, F = new WeakMap, O = new WeakMap, A = new WeakMap, 
H = new WeakMap, R = new WeakMap, N = new WeakMap;

B.style = d;

export { B as gx_ide_kb_manager_import };
//# sourceMappingURL=p-22aa1d86.entry.js.map