import { FormMetadata } from "@sankhyalabs/ezui/dist/collection/utils/form";
import { SnkMessageBuilder } from "../../../lib/message/SnkMessageBuilder";
export class GuideBuilder {
  constructor(parentGuide, formMetadata, dataUnit, messageBuilder) {
    this._parentGuide = parentGuide;
    this._formMetadata = formMetadata;
    this._dataUnit = dataUnit;
    this._messageBuilder = messageBuilder;
    if (!this._messageBuilder) {
      this._messageBuilder = new SnkMessageBuilder();
    }
  }
  get id() {
    return this._parentGuide.id;
  }
  getMessage(key, params) {
    return this._messageBuilder.getMessage(key, params);
  }
  buildGuideItem(detailBuilders) {
    const item = Object.assign({}, this._parentGuide);
    delete item.expanded;
    delete item.children;
    const currentRecord = this._dataUnit.getSelectedRecord();
    if (!currentRecord) {
      return item;
    }
    const isNewRecord = this._dataUnit.isNewRecord(currentRecord.__record__id__);
    const children = Array.from(this._formMetadata.getAllSheets().values()).map((sheet) => {
      const { name, label } = sheet;
      const isDetail = FormMetadata.getDetailName(name.split("::").pop()) != undefined;
      const disabled = isDetail && isNewRecord;
      const parentId = this._parentGuide.id;
      const id = isDetail ? `${parentId}::${name}` : `${parentId}__FORM:${name}`;
      if (isDetail && detailBuilders.has(id) && !disabled) {
        const item = detailBuilders.get(id).buildGuideItem(detailBuilders);
        return item;
      }
      return {
        id,
        label,
        disabled,
        tooltip: disabled ? this.getMessage("snkCrud.guide.tooltip.disabledDetail") : undefined
      };
    });
    const newGuideItem = children.shift();
    newGuideItem.label = isNewRecord ? this.getMessage("snkCrud.title.newRecord") : currentRecord.__record__label__;
    newGuideItem.children = children;
    item.children = [newGuideItem];
    return item;
  }
}
