/*
 * Copyright (c) 2023-2026 XMLmind Software. All rights reserved.
 *
 * Author: Hussein Shafie
 * Version: 1.8.1
 *
 * This file is part of the XMLmind XML Editor project.
 * For conditions of distribution and use, see the accompanying legal.txt file.
 */

import * as FSAccess from "./fsaccess.js";

import * as XUI from "./xui.js";

import * as XXE from "./xxeclient.js";

class LinkCalloutsDialog extends XUI.Dialog {
static showDialog(prefix, discardXRefs, lockDiscardXRefs, reference=null) {
return new Promise((resolve, reject) => {
let dialog = new LinkCalloutsDialog(prefix, discardXRefs,
lockDiscardXRefs, resolve);
dialog.open("center", reference);
});
}
constructor(prefix, discardXRefs, lockDiscardXRefs, resolve) {
super({ title: "Link Callouts",
movable: true, closeable: true, resizable: true,
template: LinkCalloutsDialog.TEMPLATE,
buttons: [ { label: "Cancel", action: "cancelAction" },
{ label: "OK", action: "okAction",
default: true } ]});
this._resolve = resolve;
let pane = this.contentPane.firstElementChild;
let subPane = pane.firstElementChild;
this._prefixText = subPane.lastElementChild;
subPane = pane.lastElementChild;
this._discardToggle = subPane.firstElementChild;
if (prefix) {
this._prefixText.value = prefix;
}
this._discardToggle.checked = discardXRefs;
this._discardToggle.disabled = lockDiscardXRefs;
}
dialogClosed(result) {
this._resolve(result);
}
cancelAction() {
this.close(null);
}
okAction() {
let result = this._prefixText.value;
if (result !== null && (result = result.trim()).length === 0) {
result = null;
}
if (result !== null && !XXE.Name.isNCName(result)) {
XUI.Alert.showError(`"${result}", not an NCName.`,
this.form);
result = null;
}
if (result === null) {
this._prefixText.focus();
this._prefixText.select();
return;
}
if (this._discardToggle.checked) {
result = "[reset] " + result;
}
this.close(result);
}
}
LinkCalloutsDialog.TEMPLATE = document.createElement("template");
LinkCalloutsDialog.TEMPLATE.innerHTML = `
<div class="xxe-docb-lc-pane">
  <div class="xxe-docb-lc-prefix">
    <label for="docb-lc-prefix">ID prefix:</label>
    <input type="text" size="30" spellcheck="false" id="docb-lc-prefix" />
  </div>
  <div class="xxe-docb-lc-hint">For example, if you specify prefix "foo",<br />
  "callout" elements will be given "foo-NUMBER" IDs and<br />
  "co"/"area" elements will be given "foo-NUMBER-co" IDs.</div>
  <div class="xxe-docb-lc-discard">
    <input type="checkbox" id="docb-lc-discard" />
    <label for="xxe-docb-lc-discard">Discard existing ID and
    "linkends"/"arearefs" attributes</label>
  </div>
</div>
`;
class LinkCalloutsCmd extends XXE.InteractiveRemoteCommand {
constructor(commandName) {
super(commandName);
}
resumeExecution(mode, docView, params,
stoppedCommandInfo, resolve, reject) {
let prefix = null;
let separ = stoppedCommandInfo.indexOf(';');
if (separ >= 0) {
prefix = stoppedCommandInfo.substring(separ+1).trim();
if (prefix.length === 0) {
prefix = null;
}
stoppedCommandInfo = stoppedCommandInfo.substring(0, separ);
}
let discardXRefs = stoppedCommandInfo.startsWith("discard");
let lockDiscardXRefs = stoppedCommandInfo.endsWith('!');
this.linkCallouts(prefix, discardXRefs, lockDiscardXRefs,
mode, docView, resolve);
}
linkCallouts(prefix, discardXRefs, lockDiscardXRefs,
mode, docView, resolve) {
LinkCalloutsDialog.showDialog(prefix, discardXRefs, lockDiscardXRefs,
docView)
.then((spec) => {
if (spec === null) {
resolve(XXE.CommandResult.CANCELED);
} else {
let result = this.execute(mode, docView, spec);
resolve(result);
}
});
}
}
for (let n of [ "docb.linkCallouts", "db5.linkCallouts" ]) {
XXE.ALL_LOCAL_COMMANDS[n] = new LinkCalloutsCmd(n);
}
