import { attribute, elementDefine, onAttributeChanged } from '../index';
import { SwcHTMLOptionElementBase } from '../elements/base/SwcHTMLOptionElementBase';

@elementDefine({ name: 'swc-if-option', extends: 'option' })
export class SwcIfOption extends SwcHTMLOptionElementBase {
  private _placeholder = document.createComment("swc-if-placeholder");
  private _isShowing = true; private _isInitialized = false;
  @attribute('swc-value') _swcValue: any = false;
  @onAttributeChanged('swc-value') onValueChange() { this.evaluate(); }
  set swcValue(val: any) { this._swcValue = val; } get swcValue(): any { return this._swcValue; }
  connectedCallback() { if (this._isInitialized) return; this._isInitialized = true; this.evaluate(); }
  private evaluate() { const attrValue = this.getAttribute('swc-value') || this.getAttribute('value'); if (attrValue !== null && attrValue.includes('{{')) return; let displayValue = attrValue !== null ? attrValue : this._swcValue; let isTruthy = !!displayValue; if (typeof displayValue === 'string') { if (displayValue === 'false' || displayValue === '0' || displayValue === '') isTruthy = false; else { try { isTruthy = !!new Function('return ' + displayValue)(); } catch (e) { isTruthy = true; } } } if (!isTruthy && this._isShowing) { if (this.parentNode) { this.parentNode.insertBefore(this._placeholder, this); this.remove(); this._isShowing = false; } } else if (isTruthy && !this._isShowing) { if (this._placeholder.parentNode) { this._placeholder.parentNode.insertBefore(this, this._placeholder); this._placeholder.remove(); this._isShowing = true; } } }
}
