export const editorBridgeEventType = {
  READY: "mkt-editor:ready",
  HOVER: "mkt-editor:hover",
  HOVER_CLEAR: "mkt-editor:hover-clear",
  SELECTION: "mkt-editor:selection",
  HOTSPOT_ACTION: "mkt-editor:hotspot-action",
  FOCUS_SELECTOR: "mkt-editor:focus-selector",
  INSPECT_SELECTOR: "mkt-editor:inspect-selector",
  START_INLINE_EDIT: "mkt-editor:start-inline-edit",
  MOVE_BLOCK: "mkt-editor:move-block",
  TOGGLE_BLOCK_VISIBILITY: "mkt-editor:toggle-block-visibility",
  DUPLICATE_BLOCK: "mkt-editor:duplicate-block",
  DELETE_BLOCK: "mkt-editor:delete-block",
  INSERT_BLOCK: "mkt-editor:insert-block",
  RESTORE_CANVAS_STATE: "mkt-editor:restore-canvas-state",
  INLINE_TEXT_CHANGE: "mkt-editor:inline-text-change",
  INLINE_ATTRIBUTE_CHANGE: "mkt-editor:inline-attribute-change",
  INLINE_STYLE_CHANGE: "mkt-editor:inline-style-change",
  BLOCK_VISIBILITY_CHANGE: "mkt-editor:block-visibility-change",
  BLOCK_STRUCTURE_CHANGE: "mkt-editor:block-structure-change",
  STATUS_SUMMARY: "mkt-editor:status-summary",
  PREVIEW_OPERATIONS: "mkt-editor:preview-operations",
}

export const normalizeCanvasSelectionPayload = (payload = {}) => ({
  selector: payload.selector || "",
  tag: payload.tag || "",
  text: payload.text || "",
  href: payload.href || "",
  src: payload.src || "",
  hidden: Boolean(payload.hidden),
  source: payload.source || "canvas-bridge",
})

export const createFocusSelectorMessage = (payload = {}) => ({
  type: editorBridgeEventType.FOCUS_SELECTOR,
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createInspectSelectorMessage = (payload = {}) => ({
  type: editorBridgeEventType.INSPECT_SELECTOR,
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createStartInlineEditMessage = (payload = {}) => ({
  type: editorBridgeEventType.START_INLINE_EDIT,
  selector: payload.selector || "",
  editKind: payload.editKind || "",
  attributeName: payload.attributeName || "",
  attributeValue: payload.attributeValue || "",
  styleProperty: payload.styleProperty || "",
  styleValue: payload.styleValue || "",
  source: payload.source || "editor-workspace",
})

export const createMoveBlockMessage = (payload = {}) => ({
  type: editorBridgeEventType.MOVE_BLOCK,
  selector: payload.selector || "",
  direction: payload.direction || "down",
  source: payload.source || "editor-workspace",
})

export const createToggleBlockVisibilityMessage = (payload = {}) => ({
  type: editorBridgeEventType.TOGGLE_BLOCK_VISIBILITY,
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createDuplicateBlockMessage = (payload = {}) => ({
  type: editorBridgeEventType.DUPLICATE_BLOCK,
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createDeleteBlockMessage = (payload = {}) => ({
  type: editorBridgeEventType.DELETE_BLOCK,
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createInsertBlockMessage = (payload = {}) => ({
  type: editorBridgeEventType.INSERT_BLOCK,
  selector: payload.selector || "",
  position: payload.position || "after",
  blockType: payload.blockType || "content-card",
  source: payload.source || "editor-workspace",
})

export const createRestoreCanvasStateMessage = (payload = {}) => ({
  type: editorBridgeEventType.RESTORE_CANVAS_STATE,
  pageHtml: payload.pageHtml || "",
  selector: payload.selector || "",
  source: payload.source || "editor-workspace",
})

export const createStatusSummaryMessage = (payload = {}) => ({
  type: editorBridgeEventType.STATUS_SUMMARY,
  mode: payload.mode || "idle",
  modeLabel: payload.modeLabel || "",
  status: payload.status || "info",
  title: payload.title || "",
  selector: payload.selector || "",
  detail: payload.detail || "",
  helper: payload.helper || "",
  primaryActionLabel: payload.primaryActionLabel || "",
  secondaryActionLabel: payload.secondaryActionLabel || "",
  source: payload.source || "editor-workspace",
})

export const createPreviewOperationsMessage = (payload = {}) => ({
  type: editorBridgeEventType.PREVIEW_OPERATIONS,
  operations: Array.isArray(payload.operations)
    ? JSON.parse(JSON.stringify(payload.operations))
    : [],
  source: payload.source || "editor-workspace",
})
