unit uDropdownHelpers;

interface

uses
  System.SysUtils, System.Classes, JS, Web,
  WEBLib.Graphics, WEBLib.Controls, WEBLib.Forms, WEBLib.Dialogs;

function GetTaskStatusClass(const AStatus: string): string;
procedure ClearTaskStatusClasses(const AElement: TJSHTMLElement);
procedure ApplyTaskStatusClass(const AElement: TJSHTMLElement; const AStatus: string);

function ExtractOptionNames(const SourceArray: TJSArray): TJSArray;
function ExtractCodeDescs(const SourceArray: TJSArray): TJSArray;
function ExtractAssignedOptionNames(const ResponseResult: TJSObject): TJSArray;
function ExtractReportedOptionNames(const ResponseResult: TJSObject): TJSArray;
function ExtractApplicationOptionNames(const ResponseResult: TJSObject): TJSArray;
function FindOptionIgnoreCase(const AItems: TJSArray; const AName: string): string;
function GetOptionsForField(const AFieldName: string; const AApplicationOptions, AReportedByOptions, AAssignedToOptions: TJSArray): TJSArray;
procedure FocusTrigger(const ATriggerId: string);


implementation



function GetTaskStatusClass(const AStatus: string): string;
var
  statusText: string;
begin
  statusText := Trim(AStatus);

  if SameText(statusText, 'Cannot Duplicate') then
    Result := 'status-cannot-duplicate'
  else if SameText(statusText, 'Cannot Test') then
    Result := 'status-cannot-test'
  else if SameText(statusText, 'Future Enhancement') then
    Result := 'status-future-enhancement'
  else if SameText(statusText, 'Fixed - Verified') then
    Result := 'status-fixed-verified'
  else if SameText(statusText, 'Fixed') then
    Result := 'status-fixed'
  else if SameText(statusText, 'Investigating') then
    Result := 'status-investigating'
  else if SameText(statusText, 'Not Fixed') then
    Result := 'status-not-fixed'
  else if SameText(statusText, 'Non-Issue') then
    Result := 'status-non-issue'
  else if SameText(statusText, 'Possibly a Problem') then
    Result := 'status-possibly-a-problem'
  else
    Result := 'status-default';
end;

procedure ClearTaskStatusClasses(const AElement: TJSHTMLElement);
begin
  if not Assigned(AElement) then
    Exit;

  AElement.classList.remove('status-cannot-duplicate');
  AElement.classList.remove('status-cannot-test');
  AElement.classList.remove('status-future-enhancement');
  AElement.classList.remove('status-fixed-verified');
  AElement.classList.remove('status-fixed');
  AElement.classList.remove('status-investigating');
  AElement.classList.remove('status-not-fixed');
  AElement.classList.remove('status-non-issue');
  AElement.classList.remove('status-possibly-a-problem');
  AElement.classList.remove('status-default');
end;

procedure ApplyTaskStatusClass(const AElement: TJSHTMLElement; const AStatus: string);
begin
  if not Assigned(AElement) then
    Exit;

  ClearTaskStatusClasses(AElement);
  AElement.classList.add(GetTaskStatusClass(AStatus));
end;

function ExtractOptionNames(const SourceArray: TJSArray): TJSArray;
var
  i: Integer;
  optionObj: TJSObject;
begin
  Result := TJSArray.new;

  if not Assigned(SourceArray) then
    Exit;

  for i := 0 to SourceArray.length - 1 do
  begin
    optionObj := TJSObject(SourceArray[i]);
    if Assigned(optionObj) then
      Result.push(string(optionObj['name']));
  end;
end;

function ExtractCodeDescs(const SourceArray: TJSArray): TJSArray;
var
  i: Integer;
  optionObj: TJSObject;
begin
  Result := TJSArray.new;

  if not Assigned(SourceArray) then
    Exit;

  for i := 0 to SourceArray.length - 1 do
  begin
    optionObj := TJSObject(SourceArray[i]);
    if Assigned(optionObj) then
      Result.push(string(optionObj['codeDesc']));
  end;
end;

function ExtractAssignedOptionNames(const ResponseResult: TJSObject): TJSArray;
begin
  if not Assigned(ResponseResult) then
    Exit(TJSArray.new);

  Result := ExtractOptionNames(TJSArray(ResponseResult['assignedToOptions']));
end;

function ExtractReportedOptionNames(const ResponseResult: TJSObject): TJSArray;
begin
  if not Assigned(ResponseResult) then
    Exit(TJSArray.new);

  Result := ExtractOptionNames(TJSArray(ResponseResult['reportedByOptions']));
end;

function ExtractApplicationOptionNames(const ResponseResult: TJSObject): TJSArray;
begin
  if not Assigned(ResponseResult) then
    Exit(TJSArray.new);

  Result := ExtractOptionNames(TJSArray(ResponseResult['applicationOptions']));
end;

function FindOptionIgnoreCase(const AItems: TJSArray; const AName: string): string;
var
  i: Integer;
  itemText: string;
begin
  Result := '';

  if not Assigned(AItems) then
    Exit;

  for i := 0 to AItems.length - 1 do
  begin
    itemText := string(AItems[i]);
    if SameText(itemText, Trim(AName)) then
      Exit(itemText);
  end;
end;

function GetOptionsForField(const AFieldName: string; const AApplicationOptions, AReportedByOptions, AAssignedToOptions: TJSArray): TJSArray;
begin
  if SameText(AFieldName, 'application') then
    Result := AApplicationOptions
  else if SameText(AFieldName, 'reportedBy') then
    Result := AReportedByOptions
  else if SameText(AFieldName, 'assignedTo') then
    Result := AAssignedToOptions
  else
    Result := nil;
end;

procedure FocusTrigger(const ATriggerId: string);
var
  el: TJSHTMLElement;
begin
  if ATriggerId = '' then
    Exit;

  el := TJSHTMLElement(document.getElementById(ATriggerId));
  if Assigned(el) then
    el.focus;
end;


end.
