import { Element } from '../models/Element';

/**
 * Create an element instance from plain object data
 * This allows us to use class methods on plain state objects
 */
export function createElementInstance(elementData) {
  return Element.fromJSON(elementData);
}

/**
 * Get element style using class method
 */
export function getElementStyle(element, charWidth, charHeight) {
  const instance = createElementInstance(element);
  return instance.getStyle(charWidth, charHeight);
}

/**
 * Get element bounds using class method
 */
export function getElementBounds(element) {
  const instance = createElementInstance(element);
  return instance.getBounds();
}

/**
 * Get element dimensions using class method
 */
export function getElementDimensions(element) {
  const instance = createElementInstance(element);
  return instance.getDimensions();
}

/**
 * Check if element intersects with rectangle
 */
export function elementIntersectsRect(element, rect) {
  const instance = createElementInstance(element);
  return instance.intersectsRect(rect);
}

/**
 * Check if text element is multi-line
 */
export function isTextMultiLine(element) {
  if (element.type !== 'text') return false;
  const instance = createElementInstance(element);
  return instance.isMultiLine();
}

/**
 * Get white-space style for text element
 */
export function getTextWhiteSpace(element) {
  if (element.type !== 'text') return 'normal';
  const instance = createElementInstance(element);
  return instance.getWhiteSpace();
}

/**
 * Check if position is on frame border
 */
export function isPositionOnFrameBorder(element, relativeCol, relativeRow) {
  if (element.type !== 'frame') return false;
  const instance = createElementInstance(element);
  return instance.isPositionOnBorder(relativeCol, relativeRow);
}
