import { ApplicationContext } from '@sankhyalabs/core';
const NO_TAB_NAME = "__NO_TAB_NAME__";
const MAIN_TAB_NAME = "__main";
export async function showConfirmMultipleUpdatePopup({ title, labelBtnConfirm, labelBtnCancel, message, changingFields, }) {
  return new Promise((resolve) => {
    const popup = builPopup(title, resolve);
    const contentContainer = buildContentContainer();
    contentContainer.appendChild(buildMessage(message));
    contentContainer.appendChild(buildChangesContainer(changingFields));
    const actionsContainer = buildActionsContainer();
    actionsContainer.appendChild(buildCancelBtn(popup, labelBtnCancel, resolve));
    actionsContainer.appendChild(buildConfirmBtn(popup, labelBtnConfirm, resolve));
    contentContainer.appendChild(actionsContainer);
    popup.appendChild(contentContainer);
    document.body.appendChild(popup);
  });
}
function groupChanges(changingFields) {
  const changesMap = new Map();
  changingFields.forEach(change => {
    var _a;
    const key = (_a = change.tabName) !== null && _a !== void 0 ? _a : NO_TAB_NAME;
    if (!changesMap.has(key))
      changesMap.set(key, []);
    changesMap.get(key).push(change);
  });
  return changesMap;
}
function getTabName(tabName) {
  const application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
  const labelPrincipal = application.messagesBuilder.getMessage("snkDataUnit.main");
  switch (tabName) {
    case NO_TAB_NAME:
      return "";
    case MAIN_TAB_NAME:
      return labelPrincipal;
    default:
      return tabName;
  }
}
function buildChangesDiv(tabName, changes) {
  const changeLabel = document.createElement('span');
  changeLabel.innerText = getTabName(tabName);
  changeLabel.style.display = 'block';
  changeLabel.style.fontWeight = 'var(--text-weight--large, 500)';
  changeLabel.style.fontSize = 'var(--text--medium, 14px)';
  changeLabel.style.marginBottom = 'var(--space--small, 6px)';
  const changeDiv = document.createElement('div');
  changeDiv.style.padding = 'var(--space--medium, 12px) 0';
  changeDiv.style.borderTop = '1px solid var(--color--disable-secondary, #f2f5f8)';
  changeDiv.appendChild(changeLabel);
  changes.forEach(change => {
    const changeLine = document.createElement('span');
    changeLine.innerHTML = `${change.label}: <span style="color: var(--text--primary, #626e82)">${change.value}</span>`;
    changeLine.style.display = 'block';
    changeLine.style.marginBottom = 'var(--space--extra-small, 3px)';
    changeDiv.appendChild(changeLine);
  });
  return changeDiv;
}
function buildChangesContainer(changingFields) {
  const changesContainer = document.createElement('div');
  changesContainer.style.flexDirection = 'column';
  changesContainer.style.display = 'flex';
  changesContainer.style.overflow = 'auto';
  changesContainer.style.maxHeight = '300px';
  const changesMap = groupChanges(changingFields);
  changesMap.forEach((changes, key) => {
    changesContainer.appendChild(buildChangesDiv(key, changes));
  });
  return changesContainer;
}
function buildMessage(messageContent) {
  const message = document.createElement('p');
  message.textContent = messageContent;
  message.style.fontSize = 'var(--text--medium, 14px)';
  return message;
}
function buildContentContainer() {
  const contentContainer = document.createElement('div');
  contentContainer.style.display = 'flex';
  contentContainer.style.flexDirection = 'column';
  contentContainer.style.fontFamily = 'var(--font-pattern, Roboto)';
  contentContainer.style.fontSize = 'var(--text--medium, 14px)';
  return contentContainer;
}
function buildActionsContainer() {
  const actionsContainer = document.createElement('div');
  actionsContainer.style.display = 'flex';
  actionsContainer.style.flexDirection = 'row';
  actionsContainer.style.justifyContent = 'flex-end';
  actionsContainer.style.gap = '5px';
  return actionsContainer;
}
function builPopup(title, resolve) {
  const popup = document.createElement('ez-popup');
  popup.opened = true;
  popup.size = 'small';
  popup.ezTitle = title;
  popup.heightMode = 'auto';
  popup.addEventListener('ezClosePopup', () => {
    document.body.removeChild(popup);
    resolve(false);
  }, { once: true });
  popup.addEventListener('ezPopupAction', () => {
    document.body.removeChild(popup);
    resolve(false);
  }, { once: true });
  return popup;
}
function buildConfirmBtn(popup, confirmBtnLabel, resolve) {
  const confirmBtn = document.createElement('ez-button');
  confirmBtn.label = confirmBtnLabel;
  confirmBtn.size = 'medium';
  confirmBtn.classList.add('ez-button--primary');
  confirmBtn.onclick = () => {
    document.body.removeChild(popup);
    resolve(true);
  };
  return confirmBtn;
}
function buildCancelBtn(popup, cancelBtnLabel, resolve) {
  const cancelBtn = document.createElement('ez-button');
  cancelBtn.label = cancelBtnLabel;
  cancelBtn.size = 'medium';
  cancelBtn.onclick = () => {
    document.body.removeChild(popup);
    resolve(false);
  };
  return cancelBtn;
}
